Skip to main content

Using an Interface as a Type

2 replies [Last post]
gjames
Offline
Joined: 2010-04-23
Points: 0

I have a question pertaining to the Java Tutorial section Using an Interface as a Type
http://java.sun.com/docs/books/tutorial/java/IandI/interfaceAsType.html

Implementing the interface Relatable in method findLargest, the Object arguments are cast to Relatable objects. I find this confusing. The Relatable interface defines nothing more than the method signature.

Can someone explain to me this interface and it's implementation?
Also, Why the cast to interface Relatable?

public interface Relatable {

// this (object calling isLargerThan) and
// other must be instances of the same class
// returns 1, 0, -1 if this is greater
// than, equal to, or less than other
public int isLargerThan(Relatable other);
}

public Object findLargest(Object object1, Object object2) {
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ( (obj1).isLargerThan(obj2) > 0)
return object1;
else
return object2;
}

___________________________________________________________________________
These methods work for any "relatable" objects, no matter what their class inheritance is. When they implement Relatable, they can be of both their own class (or superclass) type and a Relatable type. This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface.
___________________________________________________________________________

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kem099
Offline
Joined: 2010-08-11
Points: 0

Hi gjames,

I'm not a native english speaker, but I will try my best to explain clearly the way I understand it.

Suppose you have many classes that implement the Relatable interface:

[b]public class A implements Relatable { /some code/ }
public class B implements Relatable { /some code/ }
public class C implements Relatable { /some code/ }[/b]

What theses classes have in common is that you can create an instance of each of them separately, and then refer to those with an instance variable of the type Object , or the type Relatable ( Since each of them inherits from the class Object and also implements the interface Relatable).

Now you could write the findLargest method to compare an instances of classes A and class B like:

[b]public Object findLargest(A object1, B object2) {}[/b]

In this case, you would not need to cast object1 to be able to access the isLargerThan method, but you would still need to cast object2 , since isLargerThan method needs as an argument a reference variable of the type Retalable.

But you can see that this is not flexible because you would need to override the findLargest method and make different patterns if you want to compare instances of classes other than A and B ( classes that implement Relatable ).

So to be able to use the same findLargest method to compare two instances of any given classes that implement Relatable.

[b]public Object findLargest(Object object1, Object object2) {[/b]
//the arguments are reference variable of type Object
//Since every class in java inherits from Object
//you can always pass a reference variable of type A, or B, or C to the findLargest method

[b]Relatable obj1 = (Relatable)object1;[/b]
//object1 refers to an instance of A ( or B, C), but because the reference variable
//is of type Object, you cannot directly call the isLargerThan method implemented in
//the class A (or B, C).
//So you need to cast object1 to Relatable, so that you can have access to the isLargerThan method.
//but don't forget, while you can call the method isLargerThan through a reference variable of the type Relatable, the JVM will run the method implemented in the actual object ( the instance of A, B or C).

[b]Relatable obj2 = (Relatable)object2;[/b]
//Because the isLargerThan method needs a reference variable of the type Retalable to execute.

[b]}[/b]

I hope this will help.

gjames
Offline
Joined: 2010-04-23
Points: 0

kem099

I'd posted this so long ago I'd forgotten about it.

I appreciate your explanation, which I might add is excellent.

Your english is very good.

Regards,
gjames