• 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

Difference between Implementation Inheritance and interface inheriatance?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know what is Implementation inheritance but i don't know what's interface inheritance.Can anyone explain it with example?
Also i was reading a book , and it said that:

In case where you want to use implementation inheritance then it is usually provided by an abstract base class



For polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract.



I could not understand the line about interface inheritance.Also why should we prefer interface over abstract classes when we want to frequently change our design?
What i think is other way round because if we add new method suppose in a interface then we will have to define this method in every classes that implements this interface.But if we add a new method to an abstract class, then we need not define it for every class that extends this class.So where am i wrong?
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:I could not understand the line about interface inheritance. Also why should we prefer interface over abstract classes when we want to frequently change our design?


What they mean is "if you are forced to redesign your class often then that indicates that a polymorphic interface(s) would be preferable."

I think they mean that it's not desirable to be forced to redesign a base class often. Stability (of what code already exists) is the goal.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which book? You should always tell us where such quotes come from.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Which book? You should always tell us where such quotes come from.



I read it from this book:
http://www.amazon.com/Java-J2EE-Job-Interview-Companion/dp/1411668243
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Witten wrote:
What they mean is "if you are forced to redesign your class often then that indicates that a polymorphic interface(s) would be preferable."



Yes i understood that, but i wanted to know why polymorphic interface would be preferable?And also what advantages does interface have over abstract class when redesigning is the issue?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for quoting the book.
I don’t know whether our FAQ would help you. I did not think that passage from the book is very clear; it might be clearer in the context of the whole paragraph, however.

I don’t know whether this sort of example helps, but let’s try:-
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Thank you for quoting the book.
I don’t know whether our FAQ would help you. I did not think that passage from the book is very clear; it might be clearer in the context of the whole paragraph, however.

I don’t know whether this sort of example helps, but let’s try:-



I could not understand how your above code is advantageous in code reuse.Here is the full context from that book:


Q. When to use an abstract class?:
In case where you want to use implementation inheritancethen it is
usually provided by an abstract base class. Abstract classes are excellent candidates inside of application
frameworks. Abstract classes let you define some default behavior and force subclasses to provide any specific
behavior. Care should be taken not to overuse implementation inheritance as discussed in Q10in Java section.



Q. When to use an interface?:
For polymorphic interface inheritance, where the client wants to only deal with a
type and does not care about the actual implementation use interfaces. If you need to change your design
frequently, you should prefer using interface to abstract. COCoding to an interfacereduces coupling and
interface inheritance can achieve code reusewith the help of object composition. For example:The Spring
framework’s dependency injection promotes code to an interface principle. Another justification for using interfaces
is that they solve the ‘diamond problem’ of traditional multiple inheritance as shown in the figure. Java does not
support multiple inheritance. Java only supports multiple interface inheritance. Interface will solve all the
ambiguities caused by this ‘diamond problem

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can’t win them all. Maybe somebody else will have a better example.

I am a bit worried about the book saying that multiple interfaces will solve all the ambiguities of the diamond problem. Here are two examples of what it doesn’t solve:-Example 2 follows:-
 
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
I don't agree with this statement: "If you need to change your design frequently, you should prefer using interface to abstract. " I don't think there's any inherent advantage to interfaces over abstract classes when it comes to frequently changing design. And "code reuse with the help of object composition" can be achieved just as easily with abstract classes or even concrete classes as with interfaces.

And in fact, I don't even buy into the idea that there is an "interface vs. abstract class" choice. The two serve different purposes, and while there is some overlap where you could use either one, it's not that large, and to me the choice has always been obvious. Is there some reasonable common/default behavior that could be provided for at least some implementations? Then use abstract class. Are we defining a pure abstract type? Then use interface. And in many cases we use both.

This bit is also nonsense, IMHO:

Another justification for using interfaces
is that they solve the ‘diamond problem’ of traditional multiple inheritance as shown in the figure. Java does not
support multiple inheritance. Java only supports multiple interface inheritance. Interface will solve all the
ambiguities caused by this ‘diamond problem



Interfaces don't solve the diamond problem. The lack of multiple inheritance of implementation solves some of the diamond problem. But using interfaces can still have its own diamond problem, at least in theory, though I've never run across it in practice.


Our CowBartOyIst inherits from two parent types (yes, interface implementation is inheritance), so it's a potential diamond problem.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Interfaces don't solve the diamond problem. The lack of multiple inheritance of implementation solves some of the diamond problem. But using interfaces can still have its own diamond problem, at least in theory, though I've never run across it in practice.


Our CowBartOyIst inherits from two parent types (yes, interface implementation is inheritance), so it's a potential diamond problem.



Witty and instructive! Belongs in the Java Contrary Examples Hall of Fame.
 
Jeff Verdegan
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
I'm glad you like it, although I can't claim credit. Cowboy/Artist is a classic illustration of the diamond problem.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks Jeff , i got some of the things you mentioned but I didn't understand the following quote:

Jeff Verdegan wrote:. Is there some reasonable common/default behavior that could be provided for at least some implementations? Then use abstract class. Are we defining a pure abstract type? Then use interface. And in many cases we use both.



I understood about when to use abstract class but i am not able to understand this line:

Are we defining a pure abstract type? Then use interface.


Can you give an example about pure abstract type?


 
Jeff Verdegan
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

Rohan Deshmkh wrote:
I understood about when to use abstract class but i am not able to understand this line:

Are we defining a pure abstract type? Then use interface.


Can you give an example about pure abstract type?



java.util.Collection, java.util.List, java.sql.Connection, java.sql.Statement, java.sql.RestultSet.

In all cases, we define simply what this type must do. A Collection must be able to add, remove, etc. A List must be able to do everything a Collection can do, but since it is ordered, must also be able to retrieve, insert, or update elements at specific indices. And we don't care how it will be done. We're just defining what it must do.

For the java.util ones, even though the core APIs provide some implementations--and in fact there are abstract classes for Lists--they wanted to allow anybody to implement those interfaces however they want, without being bound to any particular parent class.

For the java.sql ones, the core APIs don't provide any implementation. DB vendors or possibly third parties provide libraries that can interact with the various DBs, and we don't know or care what their classes look like or how they're structured or implemented. All that matters to us is that if we have a Statement, we can call executeQuery() and get a ResultSet back, and we can in turn call next(), getXxx(), etc. on that ResultSet to get us our data.

Interfaces simply say, "If you want to call yourself X, here are the operations you must provide."
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff for explaining it with the example, i understood what you said.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic