Abstract Class Vs Interface in Java

                      Abstract Class & Interfaces in Java

Abstract is preferable

Abstract 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 preferable

Interface 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. collections like Map Set are examples of this.

Relationship perspective 

There is one more perspective of relationship as well. If there is strong relationship between base class & implementing class  we prefer Abstract class as base class so that implementing class would be type of base class. For example we say relations between bird & sparrow. Sparrow  "is a" type of Bird and fly is default behavior which is available available on bird abstract class. If there is less strong relationship like eating insect or eating plants we can use these properties like interface as some birds eat insect some not while some birds plants while some not.
and few are eating both. So eatingInsect or eatingPlant could be some interface.
So like flying is necessary quality of Bird available in all while eating habits like insect or plant is just part of their personality & could be different among them.

In a single line we can say abstract is what  a class is & interface is what a class can do or what as well as do this that etc. A Dog is a animal & it can bark Animal would be base class & bark could be interface.

As per oracle documentation

  • Consider using abstract classes if any of these statements apply to your situation:
    • You want to share code among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
  • Consider using interfaces if any of these statements apply to your situation:
    • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
    • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
    • You want to take advantage of multiple inheritance of type.

Points to remember for both

  • Java class can inherit from only one abstract class, but can implement multiple interfaces.
  • An abstract class may provide some methods with definitions – so an abstract class can have non-abstract methods with actual implementation details. An abstract class can also have constructors and instance variables as well. An interface, however, can not provide any method definitions – it can only provide method headings.
  • A class must be declared abstract when it has one or more abstract methods.
  • Abstract class cant be extended as its objective is to be inherited.
  • Methods in an abstract class can be both abstract and concrete. An abstract class can have constructors. 
  • An interface is basically a contract -- it doesn't have any implementation. An interface can contain only method declaration; it cannot contain method definition. You also can't have any member data in an interface. You can only have declaration of events, methods, and properties in an interface.


Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java