This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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

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

Shivaprakash Shettihalli

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

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 .

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3151

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

Joined: Oct 13, 2005
Posts: 43892
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: When to use Abstract class and when to use Interface ?/
It's not a secret anymore!