Interface & Abstract Class

Absract Class –
  • An abstract method is the one that is declared without an implementation (without braces, and followed by a semicolon).
  •  Abstract classes can be inherited but they cannot be instantiated.
  • It is declared with an abstract keyword. It contains both abstract and non-abstract methods (method with the body).
  • An abstract method can have constructors and static methods also.
  • In addition, it can have final methods that will force the subclass not to change the body of the method.
  • If a method in a class is declared as an abstract method then you will have to make/declare the class as an abstract class.
  • When a class inherits an abstract class then its derived class must also be declared as an abstract class.
  • If a class inherits an abstract class but the abstract method contained in the base class is overridden in the derived class then there is no need to declare the derived class as an abstract class.
Interface –
  • An interface is a reference type or a collection of abstract methods. It is used to achieve total abstraction.
  • Interfaces define what a class must do and not how. 
  • The interface exposes the features of an object. An object can access the features of another object through an interface.
  • In an interface, the inheritance feature is not available.
  • We cannot make an object of an interface but we can make a pointer of an interface.
  • Java does not support “multiple inheritance”. Multiple Inheritance can only be achieved in Java with the help of interfaces. A class can implement multiple interfaces.
  • In order to access the interface methods, the interface must be “implemented” by another class with the implements keyword.
  • Interface methods are abstract and public by default.
  • The attributes of an interface are by default public, static, and final.
  • The interface also represents the IS-A relationship.
  • If an interface extends or inherits another interface then it will have to declare the methods of another method at the time of implementation.
  • Variables can be declared in an interface but declaration and assignment of those variables have to be done at a time and then those variables become final, its value can’t be changed later.
  • When a class implements an interface then, the declared methods in the interface have to be defined in the class. And if not, then declare the class as an abstract class that is implementing the interface.
  • Extends followed by implements is the correct format. The reverse is wrong. Eg: class uuu extends ppp implements rrr,ttt