Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When do we use abstract class and interface.

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this FAQ
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic