aspose file tools*
The moose likes Beginning Java and the fly likes Where to use abstract class and Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where to use abstract class and Interface?" Watch "Where to use abstract class and Interface?" New topic
Author

Where to use abstract class and Interface?

Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Please let me know in which situation we should use abstract class and where to use Interface?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You might like to read Bill Venners' article on inheritance vs composition (with interfaces).
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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...)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to use abstract class and Interface?