• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When do we use abstract class and interface.

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this FAQ
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic