Skip to content
Related Articles

Related Articles

Improve Article

Difference between Inheritance and Interface in Java

  • Difficulty Level : Medium
  • Last Updated : 21 Jun, 2020

Java is one of the most popular and widely used programming languages. Java has been one of the most popular programming languages for many years. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In this article, we will understand the difference between the two most important concepts in java, inheritance and interface.

Interface: Interfaces are the blueprints of the classes. They specify what a class must do and not how. Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (i.e.) they only contain method signature and not the body of the method. Interfaces are used to implement a complete abstraction.

Inheritance: It is a mechanism in java by which one class is allowed to inherit the features of the another class. There are multiple inheritances possible in java. They are:

  1. Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B.

  2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.

    Multilevel-inheritance1

  3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived class B, C and D.

    Hierarchical-inheritance1

The following table describes the difference between the inheritance and interface:

CategoryInheritanceInterface
DescriptionInheritance is the mechanism in java by which one class is allowed to inherit the features of another class.Interface is the blueprint of the class. It specifies what a class must do and not how. Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
UseIt is used to get the features of another class.It is used to provide total abstraction.
Syntaxclass subclass_name extends superclass_name {
}
interface <interface_name>{
}
Number of InheritanceIt is used to provide 4 types of inheritance. (multi-level, simple, hybrid and hierarchical inheritance)It is used to provide 1 types of inheritance (multiple).
KeywordsIt uses extends keyword.It uses implements keyword.
InheritanceWe can inherit lesser classes than Interface if we use Inheritance.We can inherit enormously more classes than Inheritance, if we use Interface.
Method DefinitionMethods can be defined inside the class in case of Inheritance.Methods cannot be defined inside the class in case of Interface (except by using static and default keywords).
OverloadingIt overloads the system if we try to extend a lot of classes.System is not overloaded, no matter how many classes we implement.
Functionality ProvidedIt does not provide the functionality of loose couplingIt provides the functionality of loose coupling.
Multiple InheritanceWe cannot do multiple inheritance (causes compile time error).We can do multiple inheritance using interfaces.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :