Inheritance in Java

In this session, we will concentrate on another OOPs concept of Java which is known as Inheritance.

Objectives:

  • Introduction
  • Parent class and child class
  • Understanding with Example
  • Types of Inheritance

Introduction to Inheritance:

Inheritance is an important concept of Java where you can work with parent and child concept. It facilitates the code reusability. Code reusability is nothing  but using the same code again and again based of requirement without writing the same code again and again.  The philosophy of it is based on the life cycle of the creature. You know that child acquires some properties or characteristics from their parents. Same as this in inheritance, child class inherits the variables, methods from its parent class.

Using this, you can use the general class that defines the methods or variables which can be used in multiple child class. In java, the class which is inherited is called “Super” class or “Parent” class and the class which inherits properties from the super class is called “Derived” class or “Child” class. To support Inheritance, we need to use “extends” keywords.

Usually inheritance is called as IS – A relationship in java. we can call this relationship as parent-child as well.

Below are some of the keystone which is used in Inheritance very often.

Class : Class is the template or blueprint of the Object. It is the group of objects having the common properties.

Super class / Parent Class : This is called as Base class. This is the class which inherits the properties/ variables/ methods the other class.

Sub Class / Child Class : This is also called as Derived class as it is derived from the Super class or parent class

Reusability : This is the mechanism which helps the programmer to use the same codes multiple times rather writing the same code again and again.

Syntax:

Class sub-class name extends super-class name

{

           // Fields and Methods

}

From the above syntax, extends keyword specifies that the sub class is inherited from super class. As a result the properties of super class will be inherited to subclass and all fields and methods can be used in super class.

Example:

Class Parent {
// Java Code and Logic for parent class
}
Class Child extends Parent{
// Java code and Logic for Child class
} 

Here Parent class is super class and Child class is Sub class or derived class.

Advantage of Inheritance:

Code Reusability is the main advantage of Inheritance. In Inheritance child class or derived class replicates the members of its parent class or super class. In this way Programmer need not to write the same bunch of code again and again but he/she can re-use the same for multiple times.

Restriction in Inheritance:

If the parent class or super class member is declared as private then child class cannot replicate the class members of the parent class.

Another restriction in inheritance in java is it does not support Multiple Inheritance.

Let’s understand the basic inheritance with a simple example.

Example:

class Parent{
	void show(){
		System.out.println("This is child class method");
	}
	
}

class Child extends Parent {
	
}

class DemoInheritance {
	public static void main(String args[]){
		Child t = new Child();
		t.show();
	}
}

Explanation:

From the above example, you can notice that we have declared 3 classes. i.e. Parent, Child, DemoInheritance. Here Parent class is super class and child class is derived class. This is because we have defined the Child class with extends keyword to Parent class.

So that the show method in Parent class is inherited to the Child class. When we are trying to call the show() method through the object of Child class, it will print the “This is child class method”.

Types of Inheritance:

Types of Inheritance:

There are mainly 5 types of Inheritance in Java.

  • Single Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance
Inheritance

Let’s understand each inheritance  one by one.

Note: Java does not support Multiple Inheritance.

Single Inheritance:

From the name, you can guess what the single inheritance is. When a Java class inherits another java class then it is called as Single Inheritance.  In this type of inheritance there is only one relation is established.

Single

In the above diagram you can see that Class A inherits to the Class B. As there is only one relationship, it is called as Single Inheritance. Let’s understand single inheritance with an example.

Example:

class A{
	void showOfA(){
		System.out.println("This is class A method");
	}
	
}

class B extends A{
	void showOfB(){
		System.out.println("This is class B method");
	}
}

class DemoSingleInheritance {
	public static void main(String args[]){
		B t = new B();
		t.showOfA();
		t.showOfB();
	}
}

Output:

This is class A method
This is class B method

Explanation:

From the above example you can notice that Class A inherits to Class B and when you are trying to call the method of class A through the object of class B then the Class A method is executing and displaying the output.

Multi-Level Inheritance:

From the name, you can guess what the Multi-Level inheritance is. When a Java class inherits another java class and the inherited class inherits to another class then it is called as Multi-Level Inheritance.  In this type of inheritance there is only two relations is established.

Multi-Level Inheritance:

In the above diagram you can see that Class A inherits to the Class B and class B inherits to Class C. As there is only two relationships with multiple levels, it is called as Multi-Level Inheritance. Let’s understand Multi-Level inheritance with an example.

Example:

class A{
	void showOfA(){
		System.out.println("This is class A method");
	}
	
}

class B extends A{
	void showOfB(){
		System.out.println("This is class B method");
	}
}

class C extends B{
	void showOfC(){
		System.out.println("This is class C method");
	}
}

class DemoSingleInheritance {
	public static void main(String args[]){
		C t = new C();
		t.showOfA();
		t.showOfB();
		t.showOfC();
	}
}

Output:

This is class A method
This is class B method
This is class C method

Explanation:

From the above example you can notice that Class A inherits to Class B and Class B inherits to Class C and when you are trying to call the method of class A & B through the object of class C then the Class A method and Class B method is executing and displaying the output.

Q. Can you call the method of Class C through the object of class B in the above example?

Let’s answer this Question with the same example:

Example:

class A{
	void showOfA(){
		System.out.println("This is class A method");
	}
	
}

class B extends A{
	void showOfB(){
		System.out.println("This is class B method");
	}
}

class C extends B{
	void showOfC(){
		System.out.println("This is class C method");
	}
}

class DemoSingleInheritance {
	public static void main(String args[]){
		B t = new B();
		t.showOfA();
		t.showOfB();
		t.showOfC();
	}
}

Output:

Inheritance

Explanation:

From the above example you can notice that Class A inherits to Class B and Class B inherits to Class C.

When you are trying to call the method of class C B through the object of class B then it is throwing an compilation error Because as Class B is inherited from Class A but not C. Class B has member of its own class and Class A but not Class C.

That is why when we are trying to call class C method through the Class B object then it is throwing and compilation error.

Hierarchical Inheritance:

Like Multi-level inheritance, Hierarchical Inheritance also has two relationship. When two class is inherited from a single class then it is known as hierarchical Inheritance. Below is the graphical representation of the hierarchical Inheritance.

Hierarchical

In the above diagram you can see that Class A inherits to the Class B and class C. As there is only two relationships with hierarchical structure, it is called as Hierarchical Inheritance in Java. Let’s understand Hierarchical inheritance with an example.

Example:

class A{
	void showOfA(){
		System.out.println("This is class A method");
	}
	
}

class B extends A{
	void showOfB(){
		System.out.println("This is class B method");
	}
}

class C extends A{
	void showOfC(){
		System.out.println("This is class C method");
	}
}

class DemoSingleInheritance {
	public static void main(String args[]){
		B t = new B();
		C t1 = new C();
		t.showOfA();
		t.showOfB();
		t1.showOfC();
		t1.showOfA();	
}
}

Output:

This is class A method
This is class B method
This is class C method
This is class A method

Explanation:

From the above example you can notice that Class A inherits to Class B and Class C and when you are trying to call the method of class A & B through the object of class B and method of class A & C through the object of class C then the Class A method is executing for both Class B and Class C Object and displaying the output.

Multiple Inheritance:

When a single class is inherited from multiple class then this is known as Multiple inheritance in java.

Let’s have a look with graphical representation.

Multiple

In the above diagram you can see that Class C is inherited from the Class B and class C. But Java does not supports Multiple Inheritance.

Q. Why java does not supports Multiple Inheritance?

From the multiple inheritance, you know form the diagram that Class C is inherited from Class A and Class B. If there is a common method name declared in Class A and Class B then while inheritance which method is executed on call through the object of Class C? Yes it will not

Let’s understand this situation with an Example

Example:

class A{
	void show(){
		System.out.println("This is class A method");
	}
	
}

class B {
	void show(){
		System.out.println("This is class B method");
	}
}

class C extends A ,B {
	void showOfC(){
		System.out.println("This is class C method");
	}
}

class DemoSingleInheritance {
	public static void main(String args[]){
		C t = new C();
		t.show();}
}

Output:

Inheritance

Explanation:

In this case, Class C object will be confused which show() method to call. So this is not supported in Java.

Hybrid Inheritance:

Hybrid inheritance in java is nothing but combination of two or more inheritance discussed above. Though we cannot replicate Multiple Inheritance in java class, we cannot use hybrid Inheritance as well.

But we can achieve this through Interface. We will discuss about Interface later.

Hybrid

Some Key Points need to be remembered about Inheritance

  • We all know that Object class is a super most class in Java. So it has not parent class. Other than Object class every other class may have a direct super class (Single Inheritance). If we have not defined any then every class is a subclass of the Object class implicitly.
  • As we know that all fields, members, methods can be inherited to the sub class. But what about the constructor? Yes, Constructor is a part of the class not member of the class. So constructor is not inherited through it.
  • Private Access specifier is the most restricted access specifier in java. If a member of the class is declared as private then the same private member is not able to inherit to the subclass. But if we want to access those private member in the subclass then we need to access through some public method of super class like getter, setter etc.
  • Through Java does not support multiple Inheritance, a subclass should have only one super class. But a class may have one or more than one Sub class.
  • All the inherited method, fields etc. can be used directly in the sub class  just like normal method and fields.
  • We can use super keyword to invoke the Super Class constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *