• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces and the Java Tutorial

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. :-)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic