Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

choice between absract class and interface?

 
sunil kumre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What factors would decide as to whether an abstract class should be used instead of an interface or vice-a-versa?
thanks in advance
sunil.
 
Amit Agrawal
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one of the firsts that force me to choose between them is whether I want to keep body of any of the methods or not as method definitions are not allowed in interfaces. while you can give definition to one or all methods in an abstract class and still achieve the same purpose as of interface.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, an interface would normally be preferred where you want un-related classes (e.g. classes in different packages from different inheritance hierarchies) to be able to share some kind of functionality.
Plus, interfaces are more flexible, since a class can implement multiple interfaces, whereas only one abstract class can be extended by the subclass.
If you can see that all subclasses in a particular family will share some common implementation of functionality, that would normaly be a good case for an abstract class, since you need the same implementation of the same functionality in many places.
One way to cover your bases - if in doubt, write code for both.
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
sunil kumre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael for the wonderfuk explaination.
regds sunil.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic