What is Inheritance

In our day to day life, we use a word inherit which simply means-‘to receive from an ancestor by legal succession or will’. We use this term generally in case where a child inherits his/her parents’ properties or where a parent-child relationship exists. This process of inheriting the properties is referred as inheritance. Similarly, in java the term inheritance, is also used where a parent child relationship exists, but instead of human beings here parent as well as child is a class.

Inheritance is a concept using which a class can inherit the properties of another class. Inheritance allows the creation of hierarchal classifications and therefore is considered as one of the cornerstones of object-oriented programming. Basically, we use inheritance to achieve Dynamic Binding (discussed later) and Code Reusability. Code Reusability means that once we have created a general class which defines traits common to a set of related items, this class can be inherited by other classes, each adding those things that are unique to it. Reusing existing code saves time, money, and efforts and increases a program’s reliability.

In the terminology of java, the class that is inherited is referred as a base class or a super-class or a parent clas son the other hand the class which is inheriting is known as a child class or a sub-class. The child class inherits the proper the properties of the parent class by using the extends keyword.

Different Types of Inheritances in Java:

1) Single Inheritance: – When one sub-class inherits only one super-class, it is known as single inheritance. For example;
class Base
{
void show()
{
System.out.println(“Base class”);
}
}
class Child extends Base
{
void display()
{
System.out.prinln(“Child class”);
}
public static void main(String a[])
{
Child c=new Child();
c.display();
}
}

2) Multilevel Inheritance: – A multilevel inheritance is achieved when a super-class is inherited by one sub-class and again this sub-class is inherited by another sub-class and so on. For example;
class Base
{
void show()
{
System.out.println(“Base”);
}
}
class Child1 extends Base
{
void show1()
{
System.out.println(“Child1”);
}
}
class Child2 extends Child1
{
void display()
{
System.out.println(“Child2”);
}
public static void main(String a[])
{
Child c2=new Child();
c2.display();
Base b=new Base();
b.show();
}
}

3) Hierarchal Inheritance: – In hierarchal inheritance, we have only one super class which is inherited by more than one sub-classes.
class Base
{
void show()
{
System.out.println(“Base”);
}
}
class Child1 extends Base
{
void show1()
{
System.out.println(“Child1”);
}
}
class Child2 extends Base
{
void show2()
{
System.out.println(“Child2”);
}
public static void main(String a[])
{
Child2 c2=new Child2();
c2.show1();
Base b=new Base();
b.show();
}
}

NOTE: – All the data members and member functions of the parent class are available to the child class, unless they are declared private within the parent class.

If a data member is declared in a parent class as well as in the child class, both having the same name and if we simply use it further in our program, then by default the value of child class’ data member is used and we say that child class hides data of the parent class.

For example;
class Base
{
int x=10;
}
class Child extends Base
{
int x=20;
void show()
{
System.out.println(“x”);
}
public static void main(String a[])
{
Child c=new child();
c1.show();
}
}
Output- 20

To overcome this problem, we use the keyword ‘super’. Using super, we refer to the parent class’ data member.
We write (super.x) in line number 10 instead of x to get output as 10, i.e. the value of x in parent class.

Understand more about Inheritance in Whizlabs OCPJP 6 Training Course.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.
Scroll to Top