Posts

Showing posts from June 25, 2016

Abstract Class Vs Interface in Java

Abstract Class & Interfaces in Java

Abstract is preferableAbstract class is preferable when you want a hierarchical type framework.Abstract class provide default functionality or implementations for rest of sub classes.If you don't need default implementations go for interface.If in future you plan to add more method then abstract class should be your base class as in case of interface you will break earlier implementation & you will have to add that method in all implemented classes.


Interface is preferableInterface is preferable when your base class is not changing at all.If base class will be interface other classes which are using that interface by implementing its method will break.Interfaces are more flexible, because a class can implement multiple interfaces.  Since Java does not have multiple inheritance, using abstract classes prevents your users from using any other class hierarchy. In general, prefer interfaces when there are no default implementations or state. c…