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:
- 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.
- 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.
- 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.
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. |
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Inheritance of Interface in Java with Examples
- Difference between Inheritance and Composition in Java
- Difference between Abstract Class and Interface in Java
- Difference between Priority Inversion and Priority Inheritance
- Difference between Single and Multiple Inheritance in C++
- Difference between Inheritance and Polymorphism
- Difference between Input and Output Interface Unit
- Difference between Parallel Virtual Machine (PVM) and Message Passing Interface (MPI)
- Differences between Interface and Class in Java
- Inheritance and constructors in Java
- Comparison of Inheritance in C++ and Java
- Java and Multiple Inheritance
- Interfaces and Inheritance in Java
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | DoubleToIntFunction Interface in Java with Example
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




