Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

Abstract Classes vs Interfaces

 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am trying to understand where Interfaces should be used and where Abstract Classes. What are the advantages of one over the other.

I have read Eckel's book on this topic but don't find it quite satisfactory. Can somebody guide to some source or explain it themself.
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • Report post to moderator
Q. When to use an abstract class?: In case where you want to use implementation inheritance then 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.

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. Coding to an interface reduces coupling and interface inheritance can achieve code reuse with 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�.

Design pattern: Strategy design pattern lets you swap new algorithms and processes into your program without altering the objects that use them. Strategy design pattern: Refer Q11 in How would you go about� section.


Look at Q10 & Q12 in the following dowbload:

Free Q&A download
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Report post to moderator
Note that this question is asked about one a week on this forum, so there is not much new ground to cover here, but its worth mentioning that abstract classes can implement an interface which can give you the best of both worlds. I would say that when you get the urge to implement an abstract class you should define an interface first and let the abstract class implement that. That gives you the option of whether you want to tie your new class to the abstract class hierarchy (possibly with default implementation of some methods), or code an implementation of the interface from scratch. That way you gain a lot of freedom. All code not in the inheritance hierarchy should refer only to the interface.

I'm sure whoever coded the java.util.Stack class wishes they would have defined behavior in terms of an interface instead of directly extending java.util.Vector (slightly off-topic since Vector is not an abstract class).

Does that make sense?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't crosspost. Continue discussion here: http://www.coderanch.com/t/100533/patterns/Interfaces-vs-Abstract-Classes
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic