Friday, December 27, 2024
Google search engine
HomeLanguagesJavaDifference between Inheritance and Interface in Java

Difference between Inheritance and Interface in Java

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:

Category Inheritance Interface
Description Inheritance 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).
Use It is used to get the features of another class. It is used to provide total abstraction.
Syntax class subclass_name extends superclass_name {
}
interface <interface_name>{
}
Number of Inheritance It 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).
Keywords It uses extends keyword. It uses implements keyword.
Inheritance We can inherit lesser classes than Interface if we use Inheritance. We can inherit enormously more classes than Inheritance, if we use Interface.
Method Definition Methods 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).
Overloading It overloads the system if we try to extend a lot of classes. System is not overloaded, no matter how many classes we implement.
Functionality Provided It does not provide the functionality of loose coupling It provides the functionality of loose coupling.
Multiple Inheritance We cannot do multiple inheritance (causes compile time error). We can do multiple inheritance using interfaces.
RELATED ARTICLES

Most Popular

Recent Comments