Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Interfaces and the Java Tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces and the Java Tutorial" Watch "Interfaces and the Java Tutorial" New topic
Author

Interfaces and the Java Tutorial

Gregory Lafrance
Greenhorn

Joined: Sep 25, 2012
Posts: 2
I'm going through this Java Tutorial page on interfaces:

http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

At the end of the page, it says:

"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."

I don't fully get this "This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface."

Because the two objects that can be compared using isLargerThan(obj1, obj2) are of the same class, and that class must implement isLargerThan(). So if the class implements multiple interfaces, to satisfy the "contract" of the interfaces, that class must implement the interface methods, so they are not inheriting those methods, but are implementing the methods. Implementing the interfaces just enforces them to implement those methods.

Am I wrong on this? I can never get how interfaces allow a form of multiple inheritance, because it seems when you implement multiple interfaces, you don't "get" anything extra, you are just "required" to implement something.

Please help. :-)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gregory Lafrance wrote:
"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."

I don't fully get this "This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface."

Because the two objects that can be compared using isLargerThan(obj1, obj2) are of the same class, and that class must implement isLargerThan(). So if the class implements multiple interfaces, to satisfy the "contract" of the interfaces, that class must implement the interface methods, so they are not inheriting those methods, but are implementing the methods. Implementing the interfaces just enforces them to implement those methods.


The main purpose of inheritance is abstraction and type specialization. Beginners often think it's code sharing ("If I extend this class, I can use its methods without having to explicitly create another object.") but that's a secondary benefit (and often is actually a hindrance rather than a benefit at all).

When we implement an interface, we are inheriting that type, which means we are inheriting the fact that we provide all the methods the interface declares. The fact that we don't inherit any implementation is irrelevant. We still inherit the type and its contract.

The purpose of interfaces isn't just to force classes to implement methods for the fun of it. It's for the separation of interface from implementation, so that users only have to know and care about the abstract type they're dealing with, and don't have to be concerned with the specific implementation.
Gregory Lafrance
Greenhorn

Joined: Sep 25, 2012
Posts: 2
Thanks for your comments. The following stackoverflow post provided some additional contemplation.

http://stackoverflow.com/questions/3556652/how-do-java-interfaces-simulate-multiple-inheritance

I think interfaces and multiple-inheritance in Java is part philosophy.

But I can see some solid usefulness for interfaces in Java.

Thanks again!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Gregory Lafrance wrote:Because the two objects that can be compared using isLargerThan(obj1, obj2) are of the same class, and that class must implement isLargerThan(). So if the class implements multiple interfaces, to satisfy the "contract" of the interfaces, that class must implement the interface methods, so they are not inheriting those methods, but are implementing the methods. Implementing the interfaces just enforces them to implement those methods.


Implementing an interface doesn't suggest inheriting those methods, but it does suggest inheriting the behavior. Lets say you have the class referred to in that tutorial: RectanglePlus, which implements Relatable. You also have IntegerPlus which also implements Relatable. There really is nothing in common between RectanglePlus and IntegerPlus, but they both have an isLargerThan(Object) method, they both have a common behavior even though they don't have a common parent class. This means they can both be used in situations where you need something Relatable, but don't have about the specifics. For example, the tutorial shows the findLargest(Object, Object) method. Personally, I would define that method signature as findLargest(Relatable, Relatable) so there would be no need cast. But since the only requirement is that the two inputs are Relatable, you could do the following:

You could use the same exact findLargest(Relatable, Relatable) method to compare both RectanglePlus objects and IntegerPlus objects because they both have the same behavior (an isLargerThan(Object) method), and you know they must have that method because they implement the Relatable interface.

Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces and the Java Tutorial
 
Similar Threads
Prefering interface over class inheritance
Multiple inheritance
interface and implementation
mockexam question.
Interfaces vs Abstract Classes