wood burning stoves 2.0*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
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: 104

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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: 38075
    
  22
We have an FAQ about that; I think it’s No 21
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Shivaprakash Shettihalli wrote:When should one Abstract class and when Interface ?

See Interface Vs Abstract Class FAQ. Hope it helps.


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
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 & Interface
abstract class vs interface ?
forcing implementing methods