aspose file tools*
The moose likes Java in General and the fly likes When do we 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 » Java in General
Bookmark "When do we use abstract class and interface." Watch "When do we use abstract class and interface." New topic
Author

When do we use abstract class and interface.

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
ABSTRACT CLASS:
Use abstract class in places where you want to use implementation inheritance.
Abstract classes let you define some default behaviour and force subclasses to provide any specific behaviour. ( what does behaviour mean here ?)

INTERFACE :
Use interface when we want to avoid "diamond problem"

Any other points that can be added to the above?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
There is more to interfaces and abstract classes than just a few points.

I consider abstract classes a mixture between an interface and a concrete class.

You can both provide an implementation and also declare abstract methods that need to be implemented by any subclass.

Interfaces only provide a contract of what a class can do, no matter how it is implemented.


JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Look at this FAQ
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

Since this is a design related question, I'd think talk about one specific such pattern which deals with both Abstract classes and Interfaces requires mention. I'm indeed talking about the Strategy Design Pattern!

According to this pattern:-

All common functionality is segregating all common functionality in a hierarchy higher up to an Abstract class and then make the individual classes implement Interfaces for adding independent functionality. An example for this in real world are Vehicles. E.g. Bus, Plane, Ship e.t.c. The common functionality is implemented in the super Abstract class with the differences by implementing Interfaces.

Hope this is helpful. Any comments on this is welcome.

Cheers,
Raj.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

jose chiramal wrote:
INTERFACE :
Use interface when we want to avoid "diamond problem"


Why do you worry about *Deadly Diamond of Death* ?

By the way: Java doesnt support multiple inheritence
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Raj Kamal wrote:Hi,

Since this is a design related question, I'd think talk about one specific such pattern which deals with both Abstract classes and Interfaces requires mention. I'm indeed talking about the Strategy Design Pattern!

According to this pattern:-

All common functionality is segregating all common functionality in a hierarchy higher up to an Abstract class and then make the individual classes implement Interfaces for adding independent functionality. An example for this in real world are Vehicles. E.g. Bus, Plane, Ship e.t.c. The common functionality is implemented in the super Abstract class with the differences by implementing Interfaces.
Hope this is helpful. Any comments on this is welcome.


Hmmm. another example of Abstract class + Interface = AbstractInterface example: java.util.AbstractList
 
wood burning stoves
 
subject: When do we use abstract class and interface.