In this tutorial of Java programming, we will see the different types of inheritance in Java. I will also be explaining each type with the Java program example. Pictorial representation of each type shown in this tutorial will make your job easy to understand.
In object-oriented programming, the mechanism of acquiring the properties and functionalities of one class into another class is known as inheritance. The main intent of inheritance lies in providing reusability of code so that common properties and functionalities can be extended from the other class.
In Java, inheritance showcases the IS-A relationship, i.e., a relationship between parent and child. So, let’s see the parent and child classes first.
The class whose properties and functionalities are inherited by another class is called parent class. It is also called as a superclass or a base class.
The class which inherits the properties of the other class is called child class. It is also called as a subclass or a derived class.
The creation of a new class with the help of the properties of an already created class is the basic notion of inheritance, i.e., the data members (instance variables) and member functions (methods) of the base class can be used in derived class.
So there are many advantages of using inheritance in Java:
The extends
keyword is used to depict inheritance in Java. Here we are using Super
as the parent class and Sub
as the child class.
class Sub extends Super { //code }
Make a note that, the child class extends all the properties that are declared as public or protected. While the member declared as private can be accessed using public or protected getter and setter methods.
Java defines varied types of inheritance namely–
In object-oriented programming, there are also multiple inheritances and hybrid inheritance. Unlike other programming languages, Java does not support these multiple and hybrid inheritance. Why are they not supported? We will see that at the end of the post.
When a single child class extends the properties of the parent class, then it is said to showcase single inheritance. Single inheritance can be represented as follows.
For example,
class Shape{ void draw() { System.out.println(“Draw shape”); } } class Circle extends Shape { void drwaCircle(){ System.out.println(“Draw Circle”); } public static void main(String args[]) { Circle c = new Circle(); c.draw(); c.drawCircle(); } }
Output:
Draw shape Draw Circle
When classes extend the properties of each other level by level is known as multilevel inheritance. For instance, class Z extends Y and class Y extends X. The pictorial representation below will help you understand the gist of multi-level inheritance better.
Above is a pictorial representation of Multi-Level Inheritance.
A simple example is followed:
class Watch { void display() { System.out.println(“WATCH”); } class Titan extends Watch { void property() { System.out.println(“TITAN”); } } class Raga extends Titan { void type() { System.out.println(“RAGA”); System.out.println(“Classic Collection”); } } class TestDemo { public static void main(String args[]) { Raga r = new Raga(); r.display(); r.property(); r.type(); } }
Output:
WATCH TITAN RAGA Classic Collection
When one single class is inherited by multiple subclasses is known as hierarchical inheritance. Before we discuss an example of hierarchical inheritance, let us look at the pictorial representation of hierarchical inheritance first.
A simple example is discussed below:
class Laptop { void display() { System.out.println(“Working...”); } class Dell extends Laptop { void print() { System.out.println(“Dell Inspiron”); } } class Lenovo extends Laptop { void show() { System.out.println(“Lenovo YOGA”); } } class TestDemo { public static void main(String args[]){ Dell d = new Dell(); d.print(); d.display(); Lenovo l = new Lenovo(); l.show(); l.display(); } }
Output:
Dell Inspiron Working... Lenovo YOGA Working...
The basic gist for not supporting multiple inheritance is to avoid ambiguity caused by it. As hybrid inheritance is a mixture of the different types of inheritances that exist. Thus like multiple inheritance, hybrid inheritance also can’t be implemented.
This is all from the different types of inheritance in Java. If you have any doubt feel free to write a comment.
Thanks. I get more knowledge from this note. I’m new to Java. Following your other tutorials as well.