aspose file tools
The moose likes Java in General and the fly likes When to use Abstract class and when to use Interface ?/ Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "When to use Abstract class and when to use Interface ?/" Watch "When to use Abstract class and when to use Interface ?/" New topic
Author

When to use Abstract class and when to use Interface ?/

Shivaprakash Shettihalli
Greenhorn

Joined: Nov 20, 2007
Posts: 18
When should one Abstract class and when Interface ?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Abstract classes gives the extending subclasses a default list of functionality. If you think it will increase this default list of functionalities in the future, then you can use abstract classes. Because if that's an interface and you go and add an additional method to be supported by a class, then you break all the classes that implement that interface.

On the other hand interfaces saves a particular class from joining a hierarchy simply to achieve some behavior. You can make a class extend (if needs) from a more suitable is-a super class and mark the class to implement interfaces to achieve other desired behaviors.

I hope searching in the forum will lead you to better results.
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 95

I ran into a very good explanation of this question some time back and I have been searching for it ever since [ bookmarking it would have been the way to go ].

@John, I have seen somewhere that if two classes are not closely related yet have some behavior in common, then usually the developer needs to go with Interfaces and if the relationship between them is strong, then the developer needs to go with Abstract classes. You seem to stress on something similar. Could you please explain it further .

Thanks,
Pavan.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

If you are designing the Heirarchies among the classes then you should try to stick to Liskov Substitution Principle.

Otherwise Abstract versus Interfaces has been asked lot of times before and searching would give you some useful discussions.


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
We have an FAQ about that; I think it’s No 21
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 341
Shivaprakash Shettihalli wrote:When should one Abstract class and when Interface ?

See Interface Vs Abstract Class FAQ. Hope it helps.


http://muhammadkhojaye.blogspot.com/
 
jQuery in Action, 2nd edition
 
subject: When to use Abstract class and when to use Interface ?/
 
Similar Threads
use of abstact class and interface
When to use Interfcae and abstract class
abstract class vs interface ?
forcing implementing methods
Abstract Class & Interface