Abstract Class vs Interfaces

Abstract Class

  • An abstract class is a class that contains 0 or more abstract methods.

  • An abstract class can contain instance variables and concrete methods in addition to abstract methods.

  • We cannot declare a class as both abstract and final

  • It is possible to derive an abstract class as a subclass from a concrete super class

  • Abstract class and method is declared by using the keyword abstract.

  • All the abstract methods of the abstract class should be implemented in its subclasses

  • We cannot create an object to abstract class.

  • The reference of abstract class can not refer to individual methods of its subclasses

  • The reference of abstract class can be used to refer to objects of its subclasses

Interface

  • An interface cannot implement another interface.

  • It is possible to write a class within an interface.

  • An interface can extend another interface.

  • An interface is a specification of method prototypes.

  • An interface will have 0 to more abstract methods which are all public and abstract by default

  • None of the methods in an interface can be private, protected or static

  • We cannot create an object to an interface, but we can create a reference of interface type

  • When an interface is written, any third party vendor can provide implementation classes to it

  • Interface forces the implementation classes to implement all of its methods.

Abstract Class Vs Interfaces

Abstract ClassInterfaces
An abstract class is written when there are some common features shared by all the objectsAn interface is written when all the features are implemented differently in different objects
When an abstract class is written, it is the responsibility of the programmer to provide subclasses to itAn interface is written when the programmer wants to leave the implementation to the third-party vendors
Abstract methods and concrete methods can coexist in an abstract classAn interface contains only abstract methods
An abstract class can contain instance variable alsoAn interface can not contain instance variables
All the abstract methods of the abstract class should be implemented in its subclassesAll the methods of interface should be implemented in its implementation classes
Abstract class is declared by using the keyword abstractInterface is declared using the keyword interface

Also know when to use abstract classes and interfaces

core java programming abstract class differences interface

Subscribe For More Content