Data Structures & Algorithms using Java
Java button on computer keyboard

When a class is declared, with its specification, and the other sub-class members will want to use its member methods(functions); that’s when they will go for inheritance concepts. Inheritance concepts are everywhere in Java programming.

It’s impossible to write java programs without using inheritance. 

What is Inheritance in Java? 

Inheritance in Java Definition:

Inheritance in Java is a concept that acquires the properties from one class to other classes; for example, the relationship between father and son. 

In Java, a class can inherit attributes and methods from another class. The class that inherits the properties is known as the sub-class or the child class. The class from which the properties are inherited from is know as the superclass or the parent class.

In Inheritance, the properties of the base class is acquired by the derived classes.

Inheritance Syntax in Java:

class derived_class extends base_class  
{  
   //methods 
   //fields
} 

General format for Inheritance 

class​ superclass 
{ 
 // superclass data variables 
 // superclass member functions 
} 
class​ subclass ​extends​ superclass 
{ 
 // subclass data variables 
 // subclass member functions 
}

Inheritance uses the “extends” keyword to create a derived class by reusing base class code. 

Extends keyword in Java:

The extends keyword extends a class and is an indicator that a class is being inherited by another class. When you say class B extends a class A, it means that class B is inheriting the properties(methods, attributes) from class A. Here, class A is the superclass or parent class and class B is the sub-class or child class.

Program Example: 

class Base 
 { 
 public void M1() 
 { 
 System.out.println(“ Base Class Method ”); 
 } 
 } 
class Derived extends Base 
{ 
 public void M2() 
 { 
 System.out.printIn(“ Derived Class Methods “); 
 } 
} 
class Test 
{ 
 public static void main(String[] args) 
 { 
 Derived d = new Derived(); // creating object 
 d.M1(); // print Base Class Method 
 d.M2(); // print Derived Class Method 
 } 
} 

Output:

Output

Why use Inheritance? 

The main advantage of inheritance is code reusability and also method overriding (runtime polymorphism). 

Inheritance is also known as the IS-A relationship. 

Terms Used in Inheritance 

Class: ​A class is a collection of objects which have common properties. 

Derived Class/Sub-class: ​Derived class is a class that inherits from a base class. It is also known as subclass or child class. 

Base Class/Superclass: ​The base class is the main class where derived classes inherit the features. It is also known as the superclass or parent class. 

Reusability: ​The name itself says reuse the repeated code in the programs. It is a mechanism to reuse existing code when you are creating new classes. 

Types of Inheritance in Java 

The different types of Inheritance are:

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

Single Inheritance 

Creating subclasses from a single base class is called single inheritance. 

Single Inheritance in Java
Figure: Single Inheritance

In Inheritance, we can access superclass methods and variables. We can also access subclass methods and variables through subclass objects only. We have to take care of superclass and subclass methods, and variable’s names shouldn’t conflict. 

Program Example: 

class A 
{ 
 int a, b; 
 void display() 
 { 
 System.out.println(“Inside class A values =”+a+” ”+b); 
 } 
} 
class B extends A 
{ 
 int c; 
 void show() 
 { 
 System.out.println(“Inside Class B values=”+a+” “+b+” “+c);  } 
} 
class SingleInheritance 
{ 
 public static void main(String args[]) 
{ 
 B obj = new B(); //derived class object 
 obj.a=10; 
 obj.b=20; 
 obj.c=30; 
 obj.display(); 
 obj.show(); 
 } 
} 

Output:

Output

Multiple Inheritance in Java: 

Defining derived class from numerous base classes is known as ‘Multiple Inheritance’. In this case, there is more than one superclass, and there can be one or more subclasses. 

Multiple inheritances are available in object-oriented programming with C++, but it is not available in Java. 

Java developers want to use multiple inheritances in some cases. Fortunately, Java developers have interface concepts expecting the developers to achieve multiple inheritances by using multiple interfaces. 

Ex: class Myclass implements interface1, interface2,…. 

Multilevel Inheritance in Java:

In Multi-Level Inheritance in Java, a class extends to another class that is already extended from another class. For example, if there is a class A that extends class B and class B extends from another class C, then this scenario is known to follow Multi-level Inheritance.

We can take an example of three classes, class Vehicle, class Car and class SUV. Here, the class Vehicle is the grandfather class. The class Car extends class Vehicle and class SUV extends class Car.

Multilevel Inheritance in Java

Hierarchical Inheritance in Java:

In Hierarchical Inheritance in Java, more than one derived class extend a single base class. In simple words, more than one child class extend a single parent class or a single parent class has more than one child classes.

For example, consider a parent class Car. Now, consider child classes Audi, BMW and Mercedes. In Hierarchical Inheritance in Java, class Audi, class BMW and class Mercedes, all these three extend class Car.

Hybrid Inheritance in Java: 

Hybrid Inheritance in Java is a combination of Inheritances. In this type of Inheritance, more than one kind of inheritance is observed. For example, if we have class A and class B that extend class C and then there is another class D that extends class A, then this type of Inheritance is known as Hybrid Inheritance.

Why? Because we clearly observe that there is two kinds of inheritance here- Hierarchical and Single Inheritance.

In the diagram shown below, we see another example of Hybrid Inheritance.

 Hybrid Inheritance in Java 

Inheritance Program in Java 

  1. If we want to call methods and variables using the Parent class object, you will get an error. 

Inheritance Example in Java-1:​

class Parent 
{ 
 public void M1() 
 { 
 System.out.println("Parent Class Method"); 
 } 
} 
class Child extends Parent 
{ 
 public void M2() 
 { 
 System.out.println("Child Class Method"); 
 } 
} 
class Inh_In_Java 
{ 
 public static void main(String[] args) 
 {
  Parent p=new Parent(); 
  p.M1(); 
 ​ p.M2();​ // error-wrong way to call derived class method 
 } 
} 

Output: 

Inheritance in Java: Code Output

2. Creating object will be very important 

Parent p=new Child(); ​// will not work 

Inheritance Example in Java-2:​

class Parent 
{ 
 public void M1() 
 { 
 System.out.println("Parent Class Method"); 
 } 
} 
class Child extends Parent 
{ 
 public void M2() 
 { 
 System.out.println("Child Class Method"); 
 } 
} 
class Inh_In_Java 
{ 
 public static void main(String[] args) 
 {
 ​ Parent p=new Child(); 
  p.M1(); 
 ​ p.M2(); 
 } 
} 

Output: 

Inheritance in Java: Code Output

3. Child p=new Parent(); 

This implementation will not work because of incompatible types:  It is not possible to convert a Parent to a Child 

Inheritance Example in Java-3​

class Parent 
{ 
 public void M1() 
 { 
 System.out.println("Parent Class Method"); 
 } 
} 
class Child extends Parent 
{ 
 public void M2() 
 { 
 System.out.println("Child Class Method"); 
 } 
} 
class Inh_In_Java 
{
 public static void main(String[] args) 
 { 
 Child p=new Parent(); 
 p.M1(); 
 p.M2(); 
 } 
} 

Output: 

Inheritance in Java: Code Output

4. From the above three examples, we understand that inheritance will be useful when derived class objects call base class(parent class or superclass) methods and variables. It will not throw an error. 

Inheritance Example in Java-4: 

class Parent 
{ 
 public void M1() 
 { 
 System.out.println("Parent Class Method"); 
 } 
} 
class Child extends Parent 
{ 
 public void M2() 
 { 
 System.out.println("Child Class Method"); 
 } 
} 
class Inh_In_Java 
{ 
 public static void main(String[] args)
 { 
 Child p=new Child(); 
 p.M1(); 
 p.M2(); 
 } 
} 

Output:

Inheritance in Java: Code Output

Method Overriding in Java:

If the child class has the same method in its implementation as that of its parents class, then the concept of method overriding comes into play.

In method overriding, the child class has the same method as that of the parent class. The main use of this is to achieve runtime polymorphism.

Method Overriding is used to provide specific implementation of a particular method which was provided by the parents class.

Rules for Method overriding are:

  • Methods must share the same name in child and parent class.
  • It must have the same parameter as in the superclass.
  • There must be an IS-A type of Inheritance.

Super keyword in Java: 

Super keyword usage in inheritance, always refers to its immediate as an object. 

There are three usages of super keyword in Java: 

1. We can invoke the superclass variables. 

2. We can invoke the superclass methods. 

3. We can invoke the superclass constructor. 

Example for Super Keyword in Java: 

class Superclass 
{ 
 int i =20; 
void display() 
{ 
 System.out.println(“Superclass display method”); 
 } 
} 
class Subclass extends Superclass 
{ 
 int i = 100; 
 void display() 
 { 
 super.display(); 
 System.out.println(“Subclass display method”); 
 System.out.println(“ i value =”+i); 
 System.out.println(“superclass i value =”+super.i); 
 } 
} 
class SuperUse
{ 
 public static void main(String args[]) 
{ 
 Subclass obj = new Subclass(); 
 obj.display(); 
} 
}

Output: 

Output

Why Multiple Inheritance is not supported in Java?

Let’s consider a case in Inheritance. Consider a class A, class B and class C. Now, let class C extend class A and class B. Now, consider a method read() in both class A and class B. The method read() in class A is different from the method read() in class B. But, while inheritance happens, the compiler has difficulty in deciding on which read() to inherit. So, in order to avoid such kind of ambiguity, multiple inheritance is not supported in Java.

This brings us to the end of the blog on Inheritance in Java. Hope this helps you to up-skill your Java skills. To learn more about programming and other related concepts, check out the courses on Great Learning Academy

Also, if you are preparing for Interviews, check out these OOPS Interview questions to ace it like a pro.

1

LEAVE A REPLY

Please enter your comment!
Please enter your name here

nine + four =