aspose file tools*
The moose likes Beginning Java and the fly likes choice between absract class and interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "choice between absract class and interface?" Watch "choice between absract class and interface?" New topic
Author

choice between absract class and interface?

sunil kumre
Ranch Hand

Joined: Dec 23, 2000
Posts: 64
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

Joined: Aug 23, 2001
Posts: 282
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

Joined: Aug 22, 2001
Posts: 168
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


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
sunil kumre
Ranch Hand

Joined: Dec 23, 2000
Posts: 64
Thanks Michael for the wonderfuk explaination.
regds sunil.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: choice between absract class and interface?