Abstract class are extended where as interface are implemented. A java class can have only one parent (super class), and that parent class defines who you are. But you can implement multiple interfaces, and those interfaces define roles you can play.
A Dog object IS-A Canine, and IS-A Animal, and IS-A Object.
In other words,
Use and abstract class when you want to define a template for a group of subclasses and you have atleast some implementation code that all subclasses could use. Make the class abstract when you want to guarantee that nobody can make objects of that type.
Use an interface when you want to define a role that other classes can play regardless of where those classes are in the inheritance tree.
No comments:
Post a Comment