Interfaces and Inheritance in Java
Last Updated :
11 Sep, 2023
A class can extend another class and can implement one and more than one Java interface. Also, this topic has a major influence on the concept of Java and Multiple Inheritance.
Note: This Hierarchy will be followed in the same way, we cannot reverse the hierarchy while inheritance in Java. This means that we cannot implement a class from the interface because Interface-to-Class inheritance is not allowed, and it goes against the fundamental principles of class-based inheritance. This will also breach the relationship known as the Is-A relationship, where a subclass is a more specialized version of its superclass.
Example:
Java
import java.io.*;
interface intfA {
void m1();
}
interface intfB {
void m2();
}
class sample implements intfA, intfB {
@Override public void m1()
{
System.out.println( "Welcome: inside the method m1" );
}
@Override public void m2()
{
System.out.println( "Welcome: inside the method m2" );
}
}
class GFG {
public static void main(String[] args)
{
sample ob1 = new sample();
ob1.m1();
ob1.m2();
}
}
|
Output:
Welcome: inside the method m1
Welcome: inside the method m2
Interface inheritance: An Interface can extend another interface.
Inheritance is inheriting the properties of the parent class into the child class.
- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
- The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class.
- You can also add new methods and fields in your current class.
- Inheritance represents the IS-A relationship which is also known as the parent-child relationship.
Example
Dog IS_A Animal
Car IS_A Vehicle
Employee IS_A Person
Surgeon IS_A Doctor etc.
Below is the implementation of the above method:
Java
class Animal {
public void eat()
{
System.out.println( "Animal is eating" );
}
}
class Dog extends Animal {
public static void main(String args[])
{
Dog d = new Dog();
d.eat();
}
}
|
Syntax of Java Inheritance
class <Subclass-name> extends <Superclass-name>
{
//methods and fields
}
Note: The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.
Example of Java Inheritance
Below is the implementation of Java Inheritance:
Java
class Person1 {
int id;
String name;
void set_Person( int id, String name)
{
try {
this .id = id;
this .name = name;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void disp_Person()
{
System.out.print(id + "\t" + name + "\t" );
}
}
class Employee1 extends Person1 {
int sal;
String desgn;
void set_Emp( int id, String name, String desgn, int sal)
{
try {
set_Person(id, name);
this .desgn = desgn;
this .sal = sal;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void disp_Emp()
{
disp_Person();
System.out.print(desgn + "\t" + sal);
}
public static void main(String args[])
{
Employee1 e1 = new Employee1();
e1.set_Emp( 1001 , "Manjeet" , "AP" , 20000 );
e1.disp_Emp();
}
}
|
Output
1001 Manjeet AP 20000
Types of inheritance in Java
- Java supports three types of inheritance in Java: single-level, multilevel, and hierarchical inheritance in the case of classes to avoid ambiguity.
- In Java programming, multiple and hybrid inheritance is supported through the interface only.
1. Single Inheritance Example
When a class inherits another class, it is known as a single inheritance.
Java
class A {
int a;
void set_A( int x) {
a = x;
}
}
class B extends A {
int b, product;
void set_B( int x) {
b = x;
}
void cal_Product()
{
product = a * b;
System.out.println( "Product = " + product);
}
public static void main(String[] args)
{
B b = new B();
b.set_A( 5 );
b.set_B( 5 );
b.cal_Product();
}
}
|
2. Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance.
Below is the implementation of the above method:
Java
class A {
int a;
void set_A( int x) {
a = x;
}
}
class B extends A {
int b;
void set_B( int x) {
b = x;
}
}
class C extends B {
int c, product;
void cal_Product()
{
product = a * b;
System.out.println( "Product = " + product);
}
public static void main(String[] args)
{
C c = new C();
c.set_A( 5 );
c.set_B( 5 );
c.cal_Product();
}
}
|
3. Hierarchical Inheritance Example
When two or more classes inherit a single class, it is known as hierarchical inheritance.
Below is the implementation of the mentioned topic:
Java
class A {
int a;
void set_A( int x) {
a = x;
System.out.println( "Setting A's value to = " + x);
}
}
class B extends A {
int b;
void set_B( int x) {
b = x;
System.out.println( "Setting B's value to = " + b);
}
}
class C extends A {
int c;
void set_C( int x) {
c = x;
System.out.println( "Setting C's value to = " + c);
}
}
public class GFG {
public static void main(String[] args) {
C c = new C();
c.set_C( 5 );
c.set_A( 50 );
B b = new B();
b.set_B( 10 );
b.set_A( 15 );
}
}
|
Output
Setting C's value to = 5
Setting A's value to = 50
Setting B's value to = 10
Setting A's value to = 15
4. Inheritance in Interfaces
Java
import java.io.*;
interface intfA {
void geekName();
}
interface intfB extends intfA {
void geekInstitute();
}
class sample implements intfB {
@Override public void geekName()
{
System.out.println( "Rohit" );
}
@Override public void geekInstitute()
{
System.out.println( "JIIT" );
}
public static void main(String[] args)
{
sample ob1 = new sample();
ob1.geekName();
ob1.geekInstitute();
}
}
|
Output:
Rohit
JIIT
An interface can also extend multiple interfaces.
Java
import java.io.*;
interface intfA {
void geekName();
}
interface intfB {
void geekInstitute();
}
interface intfC extends intfA, intfB {
void geekBranch();
}
class sample implements intfC {
public void geekName() { System.out.println( "Rohit" ); }
public void geekInstitute()
{
System.out.println( "JIIT" );
}
public void geekBranch() { System.out.println( "CSE" ); }
public static void main(String[] args)
{
sample ob1 = new sample();
ob1.geekName();
ob1.geekInstitute();
ob1.geekBranch();
}
}
|
Q. Why Multiple Inheritance is not supported through a class in Java, but it can be possible through the interface?
Multiple Inheritance is not supported by class because of ambiguity. In the case of interface, there is no ambiguity because the implementation of the method(s) is provided by the implementing class up to Java 7. From Java 8, interfaces also have implementations of methods. So if a class implements two or more interfaces having the same method signature with implementation, it is mandated to implement the method in class also.
Refer to Java and Multiple Inheritance for details.