7 Replies - 24104 Views - Last Post: 09 February 2011 - 10:01 AM

#1 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Interface inheritance vs. implementation inheritance

Post icon  Posted 08 February 2011 - 05:17 AM

James Gosling (Java's inventor) reportedly said that should he do Java over again he'd do without classes, and clarified that by "classes" he meant implementation inheritance (the extends relationship). Interface inheritance (the implements relationship) is preferable.

Do you agree? Why do you think interface inheritance is preferable?
Can you give an example where interfcaes perform better?
Is This A Good Question/Topic? 4
  • +

Replies To: Interface inheritance vs. implementation inheritance

#2 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2397
  • View blog
  • Posts: 5,030
  • Joined: 11-December 07

Re: Interface inheritance vs. implementation inheritance

Posted 08 February 2011 - 06:27 AM

Effective Java has a good chapter on this. In short, it's better to use composition over inheritance when it comes to implementation. This way you can make really small modular bits that larger classes can delegate to.
Was This Post Helpful? 1
  • +
  • -

#3 SpeedisaVirus   User is offline

  • Baller

Reputation: 115
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Interface inheritance vs. implementation inheritance

Posted 08 February 2011 - 09:23 AM

I think it would be an error to not permit implementation inheritance entirely but more than often is used in situations where it is not the best solution.
Was This Post Helpful? 3
  • +
  • -

#4 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 604
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: Interface inheritance vs. implementation inheritance

Posted 08 February 2011 - 10:27 AM

The main issue with implementation inheritance is the so-called "fragile base class" problem. This problem arises when a change to the implementation of the base class causes errors in classes inheriting from it. There is nothing inherent in the implementation inheritance model that prevents this, so it is up to the individual classes themselves to avoid it. This of course limits either the changeability of a base class or its ability to scale: the more you change a base class the more likely it is to break its inherited members.

By specifying only how the functionality is to be accessed, rather than providing the functionality itself, interface inheritance avoids this problem. Each implementer of the interface provides its own implementation of the base functionality.

A side issue that illustrates the problem is the fact that Java doesn't allow multiple inheritance, but does allow multiple interface implementation. The problem with multiple inheritance is that two different base classes could provide the same method name, and therefore you would have two different implementations of the same method. Of course, there are ways to deal with that (overriding and/or overloading, for example), but the Java architects decided (probably correctly) that there wasn't much added usefulness, while there was the potential for a lot of added mischief, in allowing multiple inheritance. Since it matters not at all if two different interfaces specify the same method (a single implementation satisfies the requirements of both interfaces), multiple interface implementation is fine.

Personally, I would not say that either one is better than the other. If there is a stable piece of functionality that isn't subject to change, it makes sense to implement it in one place and let everything inherit it. (Of course, it might make MORE sense to use composition here! But I'm restricting myself to inheritance, so I'm not going to think that through just now. I'll leave cfoley to respond to that if h/s desires.) If the implementation is volatile, but the way that you access it isn't, then it makes sense to use interface implementation. Finally, there could be mixtures of the two as well. If so, then making some of the functionality abstract, and some concrete, is what to do.

HTH

This post has been edited by BobRodes: 08 February 2011 - 10:29 AM

Was This Post Helpful? 3
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Interface inheritance vs. implementation inheritance

Posted 08 February 2011 - 03:45 PM

I have to agree with BobRodes. Interface inheritance (as interfaces are defined now) would make the code a lot more loose, and force the programmer to deal with a lot more of the fragility of the code. It also takes away the strong is-a relationships gotten from class inheritance. For example, things like GeometricObjects should be strongly related through extends inheritance. An interface style inheritance doesn't allow for as much code modularity (ie., instance variables). Keep in mind that the Java GUI is built off of extends inheritance. Take that away, and it would be a lot more complicated to do even the cookie cutter development that handles 90% of any GUI work in standard Java SE.
Was This Post Helpful? 1
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Interface inheritance vs. implementation inheritance

Posted 08 February 2011 - 04:26 PM

I certainly like interfaces. They do make life easier and allow for multiple inheritance. However, I have to disagree with replacing implementations at all. Interfaces are good for standardizing the methods of public access, but implementation is useful too.

Look at ArrayList. It implements the List interface, but it inherits all those methods through it's superclass, AbstractList. It also extends other interfaces though, which it does implement, like Serialiazable. Without inheritance the way we currently have it, ALL of the List, Serializable and other interfaces would have to be rewritten. This goes against the total idea of reuse.
Was This Post Helpful? 1
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Interface inheritance vs. implementation inheritance

Posted 08 February 2011 - 05:49 PM

For me it would get annoying very quickly, having to implement IToString on everything. Some things are just better in a base class. If you want to change how something is implemented override it. Or in the extreme case remove, you could resort to using reflection.

This post has been edited by AdamSpeight2008: 08 February 2011 - 05:54 PM

Was This Post Helpful? 1
  • +
  • -

#8 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Interface inheritance vs. implementation inheritance

Posted 09 February 2011 - 10:01 AM

I think that the extends relationship is a vital part of Java, and altough interfaces offer much more flexibility and modularity to the code, They can't fully replace the extends relationship in Java.
However, a small example to understand how interfaces are powerful:
Instead of declaring a LinkedList object, declare it as List. (thus, if later you want to change it to ArrayList, changes to the code are minor).
List<SomeObject> list = new LinkedList<SomeObject>();
list.add(..);
list.remove(..);


Also, you can consider declaring it as a Collection instead (if the code allows it) and then you can really assign any Collection to it (Map, List etc).
Collection<Someobject> col = new LinkedList<SomeObject>();
col.add(..);
col.remove(..);


The above examples are trivial, and I'm sure most of the programmers are familiar with them. However, perhaps the APIs we write could be much more flexible and reusable if we used interfaces more frequently.

This post has been edited by japanir: 09 February 2011 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1