Inheritance in Java

It is always recommended if we can reuse something that already exists rather than creating the same again. With the help of Inheritance, we can achieve this concept in Java. Java classes can be reused in many ways.

Inheritance is a mechanism wherein a new class is derived from an existing class. A class may inherit or reuse the properties and methods of other class.

A class derived from another class is called a subclass or child class where is the class from which a subclass is derived is called a superclass or parent class.

Inheritance in Java - Java Point Tutorial

Java says a child has only one parent class where is a parent may have one or more child classes.

Inheritance is a compile-time mechanism and shows a static relationship between classes.

A superclass can have any number of subclasses.

But a subclass can have only one superclass. This is because Java does not support multiple inheritances.

Defining a subclass

A class can be subclassed in this manner

package test;

public class SubInstance extends SuperInstance {

Variables declaration;
Methods declaration;

}

The keyword extends specifies that the properties of the extended class will be included in that class.

The relationship between parent and child is referred to as the Is-A relationship.
The keyword extends is used to derive a subclass from the superclass.

Types of Inheritance in Java

Java supports the following types of inheritance

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

Types of Inheritance in Java , Single inheritance in java, Multilevel inheritance in java, Hierarchical inheritance in java

Single Inheritance in Java

When a class has only one superclass, is called single inheritance.

Single Inheritance in Java, Inheritance in Java

Example of Java Single Inheritance

Note: Compile javac Animal.java & Run: Java Test

class Animal{  
void eat(){System.out.println("Eating...");}  
}  
class Cat extends Animal{  
void bark(){System.out.println("Barking...");}  
}  
class Test{  
public static void main(String args[]){  
Cat c=new Cat();  
c.bark();  
c.eat();  
}}
Example Output

Multilevel Inheritance in Java

When a class has several superclasses than this type of inheritance is called as Multilevel Inheritance.

Multilevel Inheritance in Java, Inheritance in Java

Example of Java Multilevel Inheritance

Note: Compile javac Animal.java & Run: Java Test

class Animal{  
void eat(){System.out.println("Eating...");}  
}  
class Cat extends Animal{  
void bark(){System.out.println("Barking...");}  
}  
class Lion extends Cat{  
void weep(){System.out.println("Weeping...");}  
}  
class Test{  
public static void main(String args[]){  
Lion l=new Lion();  
l.weep();  
l.bark();  
l.eat();  
}
}
Example Output

Hierarchical Inheritance in Java

When a class is inherited by two different classes, that type of inheritance is known as Hierarchical Inheritance.


Hierarchical Inheritance in Java, Inheritance in Java

Example of Java Hierarchical Inheritance

Note: Compile javac Animal.java & Run: Java Test

class Animal{  
void eat(){System.out.println("Eating...");}  
}  
class Monkey extends Animal{  
void bark(){System.out.println("Barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("Meowing...");}  
}  
class Test{  
public static void main(String args[]){  
Cat c=new Cat();  
c.meow();  
c.eat();  
//c.bark();//C.T.Error  
}}
Example Output

Is-A Relationship and Has-A relationship

These types of relationships are mainly used for code reusability. In the concept of accessing class behavior and properties, we can have two types of relationships between classes-

  • Is-A relationship

  • Has-A relationship

An Is-A relationship is known as inheritance and a Has-A relationship is also known as composition.

Is-A Relationship in Java

It depends upon the concept of Inheritance. When a class in a type of super class than this type of relationship is known as Is-A relationship.

For example, a mobile phone is a device, a car is a vehicle, a tomato is a vegetable

Has-A relationship in Java

A Has-A relationship is also known as composition. When a class object is been created inside another class and we are accessing behavior of that class using the class object, this type of relationship is known as Has-A relationship.

A Has-A relationship simply means a class has an instance of another class or another instance of the same class. We commonly use a new keyword to use Has-A relationship.

For example, a car has an engine, a hotel has an address.

Share