Java – Object Oriented Programming Concepts – Inheritance


What is inheritance ?

The thing that is inherited from other is called inheritance.

What is inheritance in java ?

It is the process in java by which one class is allow to inherit the fields and methods of another class.

Why inheritance is used in java ?

Inheritance allows us to reuse the code, and improves the reusability of code in your java application.

We can create new classes that are built upon existing classes. 

When you inherit from an existing class, we can reuse methods and fields of parent class.

Also we can add new methods and fields also.

Inheritance is also known as parent-child relationship.

Important Terms in Inheritance :

1. Class :

Class is a template or blueprint for which objects are created, It is a combination of objects having common properties.

2. Sub-Class / Child-Class :

Its is also called as derived class as it inherits the fields and methods from Parent class.


3. Parent Class :

It is the base class or parent class from which child-class will inherits fields, methods and variables.

Syntax :

class Parentclass-name extends Childclass-name  
{  
   //methods and fields  
}  

“extends” – java keyword denotes new parent-class that derives from an child-class.

Example 1 :

public class ChildClass extends ParentClass{
public static void main(String args[])
{
int a = 10;
System.out.println("The value of a = "+a);
ChildClass obj = new ChildClass();
System.out.println("Printing the value of name from the parent class ... Name = "+obj.Name);
obj.Addition(100,200);
}
}
class ParentClass{
String Name = "Abdul Kalam";
public void Addition(int x, int y)
{
int z = x+y;
System.out.println("Printing the value inside the method from the parent class... value of z = "+z);

}
}

Output :

The value of a = 10

Printing the value of name from the parent class … Name = Abdul Kalam

Printing the value inside the method from the parent class… value of z = 300

Types of Inheritance :

There are 5 types of inheritance.

1. Single Inheritance

2. Multiple Inheritance

3. MultiLevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

1. Single Inheritance :

When a Child class extends another one class only then we  call it a single inheritance. 

Class A extends ClassB ( Single Inheritance )

Example 1 :

public class ChildClass extends ParentClass{
        public static void main(String args[])
        {
                int a = 10;
                System.out.println("The value of a = "+a);
                ChildClass obj = new ChildClass();
                System.out.println("Printing the value of name from the parent class ... Name = "+obj.Name);
                obj.Addition(100,200);
        }
}
class ParentClass{
        String Name = "Abdul Kalam";
        public void Addition(int x, int y)
        {
                int z = x+y;
                System.out.println("Printing the value inside the method from the parent class... value of z = "+z);

        }
}

Output :

The value of a = 10

Printing the value of name from the parent class … Name = Abdul Kalam

Printing the value inside the method from the parent class… value of z = 300

2.Multiple Inheritance :

When one class extending more than one base class then we call it as Multiple Inheritance.

Multiple Inheritance is rarely used concept in software programming.

Java doesn’t allow multiple inheritance to avoid the unwanted complexity caused by it.

Class A extends ClassB and also ClassC ( Multiple Inheritance )

3. Muti-level Inheritance :

When a class extends a class, which extends another class then this is called multilevel inheritance. 

Example 1 :

public class Child2 extends Child1{
public static void main(String args[])
{
int a = 10;
System.out.println("The value of a = "+a);
Child2 obj = new Child2();
System.out.println("Printing the Child1 variable b = "+obj.b);
System.out.println("Printing the ParentClass variable c = "+obj.c);
}
}
class Child1 extends ParentClass{
int b = 200;
}
class ParentClass{
int c = 500;
}

Output :

The value of a = 10

Printing the Child1 variable b = 200

Printing the ParentClass variable c = 500

4. Hierarchal Inheritance :

When more than one classes inherit a same class then this is called hierarchical inheritance.

Example 1 :

public class ClassA{
public static void main(String args[])
{
ClassB obj = new ClassB();
System.out.println("Printing the value from ClassB Name = "+obj.Name1);
System.out.println("Printing the value from extended ClassE Name = "+obj.Name4);
System.out.println("------------------------------------------------------------");
ClassC obj1 = new ClassC();
System.out.println("Printing the value from ClassC Name = "+obj1.Name2);
System.out.println("Printing the value from extened ClassE Name = "+obj1.Name4);
System.out.println("------------------------------------------------------------");
ClassD obj2 = new ClassD();
System.out.println("Printing the value from ClassD Name = "+obj2.Name3);
System.out.println("Printing the value from extened ClassE Name = "+obj2.Name4);
System.out.println("------------------------------------------------------------");
ClassE obj4 = new ClassE();
System.out.println("Printing the value from ClassE Name = "+obj4.Name4);
System.out.println("------------------------------------------------------------");
}
}
class ClassB extends ClassE{
String Name1 = "Abdul Kalam";
}
class ClassC extends ClassE{
String Name2 = "Arnold";
}
class ClassD extends ClassE{
String Name3 = "Teresa";
}
class ClassE{
String Name4 = "God";
}

Output :

Printing the value from ClassB Name = Abdul Kalam

Printing the value from extended ClassE Name = God

————————————————————

Printing the value from ClassC Name = Arnold

Printing the value from extened ClassE Name = God

————————————————————

Printing the value from ClassD Name = Teresa

Printing the value from extened ClassE Name = God

————————————————————

Printing the value from ClassE Name = God

5. Hybrid Inheritance :

A hybrid inheritance is a combination of more than one types of inheritance. 

Suppose if classA and classB extends classC and another classD extends classA then this is a hybrid inheritance.

above case is the combination of single and hierarchical inheritance.

Example 1 :


public class ClassA{
public static void main(String args[])
{
ClassB obj1 = new ClassB();
System.out.println("------- ClassB extended to ClassF ----- 1st Inheritance ");
System.out.println(" ");
System.out.println("Printing the values from ClassB DescB = "+obj1.DescB);
System.out.println("Printing the values from ClassF DescF = "+obj1.DescF);
System.out.println(" ");

ClassC obj2 = new ClassC();
System.out.println("------- ClassC extended to ClassF ----- 1st Inheritance ");
System.out.println(" ");
System.out.println("Printing the values from ClassC DescC = "+obj2.DescC);
System.out.println("Printing the values from ClassF DescF = "+obj2.DescF);
System.out.println(" ");

ClassD obj3 = new ClassD();
System.out.println("------- ClassD extended to ClassF ----- 1st Inheritance ");
System.out.println(" ");
System.out.println("Printing the values from ClassD DescC = "+obj3.DescD);
System.out.println("Printing the values from ClassF DescF = "+obj3.DescF);
System.out.println(" ");

ClassE obj4 = new ClassE();
System.out.println("------- ClassE extended to ClassB ----- 2nd Inheritance ");
System.out.println(" ");
System.out.println("Printing the values from ClassE DescC = "+obj4.DescE);
System.out.println("Printing the values from ClassB DescB = "+obj4.DescB);
System.out.println("Printing the values from ClassF DescF = "+obj4.DescF);
System.out.println(" ");

ClassF obj5 = new ClassF();
System.out.println("------- ClassF -----");
System.out.println(" ");
System.out.println("Printing the values from ClassF DescF = "+obj5.DescF);
System.out.println(" ");
}
}
class ClassB extends ClassF{
String DescB = "Description for ClassB";
}
class ClassC extends ClassF{
String DescC = "Description for ClassC";
}
class ClassD extends ClassF{
String DescD = "Description for ClassD";
}
class ClassE extends ClassB{
String DescE = "Description for ClassE";
} 
class ClassF{
String DescF = "Description for ClassF";
}

Output :

——- ClassB extended to ClassF —– 1st Inheritance 

Printing the values from ClassB DescB = Description for ClassB

Printing the values from ClassF DescF = Description for ClassF

——- ClassC extended to ClassF —– 1st Inheritance 

Printing the values from ClassC DescC = Description for ClassC

Printing the values from ClassF DescF = Description for ClassF


——- ClassD extended to ClassF —– 1st Inheritance 

Printing the values from ClassD DescC = Description for ClassD

Printing the values from ClassF DescF = Description for ClassF

——- ClassE extended to ClassB —– 2nd Inheritance 

Printing the values from ClassE DescC = Description for ClassE

Printing the values from ClassB DescB = Description for ClassB

Printing the values from ClassF DescF = Description for ClassF

——- ClassF —–

Printing the values from ClassF DescF = Description for ClassF