Inheritance concepts are everywhere in Java programming. It’s impossible to write java programs without using inheritance. 

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. 

What is Inheritance? 

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

The inheritance concept is acquired from the base class properties to derived classes.

fig: Inheritance concept 

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. 

Program Example: 

class Base 
 { 
 public void M1() 
 { 
 System.out.println(“ Base Class Method ”); 
 } 
 } 
 class Derived extends Base 
{ 
 public void M2() 
 { 
 System.out.println(“ 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:

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 

There are two types of Inheritance 

1. Single Inheritance 

2. Multiple Inheritance 

Single Inheritance 

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

Fig: 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:

super: 

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

There are three usages of super keyword 

1. We can invoke the superclass variables. 

2. We can invoke the superclass methods. 

3. We can invoke the superclass constructor. 

Example: 

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: 

Multiple Inheritance: 

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,…. 

There are different types of multiple inheritances in C++ 

● Multiple Inheritance 

● Multilevel Inheritance 

● Hierarchical Inheritance 

● Hybrid Inheritance

 Fig1: Multilevel Inheritance 

Fig2: Multiple Inheritance 

Fig3: Hierarchical Inheritance

Fig4: Hybrid Inheritance 

Need to understand inheritance in details 

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

Example-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: 

2. Creating object will be very important 

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

Example-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: 

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 

Example-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: 

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. 

Example-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:

0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

2 + 11 =