Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shivaprakash Shettihalli
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When should one Abstract class and when Interface ?
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have an FAQ about that; I think it’s No 21
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivaprakash Shettihalli wrote:When should one Abstract class and when Interface ?

See Interface Vs Abstract Class FAQ. Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic