• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Where to use abstract class and Interface?

 
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please let me know in which situation we should use abstract class and where to use Interface?
 
Saloon Keeper
Posts: 14095
319
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, prefer interface. Their use is more flexible; users can easily make their classes implement multiple interfaces.

Abstract classes have the advantage that they can supply default implementations, and they can easily add new methods without breaking subtypes.

When you find yourself creating an abstract class, you should also provide an interface it implements. This gives your clients the choice whether they want to use the interface or the abstract class. When you design an interface, make sure you plan it out well. Once an interface is released, it's impossible to change.

Two counter-examples from the standard API: the Observable and InputStream classes should have been interfaces instead of classes. In my opinion this was a huge oversight.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might like to read Bill Venners' article on inheritance vs composition (with interfaces).
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often use both. I define the interface, and then provide an abstract base class that implements common functions, leaving the specialization to the subclasses that "implement" the interface and inherit/extend the abstract base.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:I often use both. I define the interface, and then provide an abstract base class that implements common functions, leaving the specialization to the subclasses that "implement" the interface and inherit/extend the abstract base.


this is what exactly effective java recommended , and named as skeletal implementation of an interface . often called Abstract Interface.
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Abstract classes have the advantage that they can supply default implementations, and they can easily add new methods without breaking subtypes.


This approach is used within the collections framework; interfaces Collection, List, Set and Map with skeleton implementations AbstractCollection, AbstractList, AbstractSet and AbstractMap.

Two counter-examples from the standard API: the Observable and InputStream classes should have been interfaces instead of classes. In my opinion this was a huge oversight.


+5. Those two have been on my "rant list" for ages. I even wrote my own copies of Observable / Observer, this time with generics built in, and with Observable being an interface with abstract implementation class, and even an ObservableHelper that

can be used in classes that need to implements Observable but already extend a different class.

(direct quote from my Javadoc including the little grammar mistake I saw just now...)
 
Stephan van Hulst
Saloon Keeper
Posts: 14095
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:This approach is used within the collections framework; interfaces Collection, List, Set and Map with skeleton implementations AbstractCollection, AbstractList, AbstractSet and AbstractMap.


The collection framework is a dream to work with. This is a truly well designed API.

I even wrote my own copies of Observable / Observer, this time with generics built in, and with Observable being an interface with abstract implementation class, and even an ObservableHelper that

can be used in classes that need to implements Observable but already extend a different class.


Awesome. I'm very curious about the generics part. Do you have a link to your Javadoc?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic