What is Inheritance in OOP (Object Oriented Programming)

What Is Inheritance in OOP?

To understand inheritance we need to understand these terminologies.
1. Inheritance in OOP.
2. Inheritance in Java.
3. Types of Inheritance.
4. extend Keyword.
5. super Keyword.
6. this Keyword.
7. Encapsulation, Access Modifiers (default, public, private, protected).

  1. Inheritance in OOP:

Inheritance in OOP means when a class uses the properties of another class. In other words, if two classes have some same properties then we only define the common properties in one class known as parent class but use them in both classes.

In inheritance there is a Super class also called Parent or Root class and the other class which uses the properties and functions of that parent or super class is the Sub Class or child class.
There are many reasons of inheritance depend on the scenario like some time two classes A and B have same attributes.

Now as in the above code we see both classes have same properties, so we don’t have to define these attributes in both classes, and we use inheritance. One solution is that we define these functions in class A and inherit class B. Another solution is that we make a super class having these functions and both A and B inherit from that Super class.

OR

 


2. Inheritance in Java:

As Java is a pure object-oriented programming language so, it uses classes. Java supports three types of Inheritance: Single, Multilevel, and Hierarchal. Multiple Inheritance is not supported in Java.

Example:

 


3. Types of Inheritance:

There are about four types of Inheritance:

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritances
  • Hierarchal Inheritance

a. Single Inheritance:

In single inheritance, only one class is inherited from the parent. Like class B extends class A.

single inheritance
Single inheritance
Example:

b. Multilevel Inheritance:

In this type one class extends another and that another class is the child of some other class. Like A from B from C from D.

multilevel inheritance
Multilevel inheritance
Example:

 

c. Multiple Inheritance:

In this type one class is inherit from two or more than two super classes .like A is inherit from B and C. It’s not supported in Java.

 

d. Hierarchal Inheritance:

In this type from one super class, many subclasses are inherited, and many classes use the attributes and functions of that super class. Like A is a superclass and B, C and D are inherit from it.

hierarical inheritance
Hierarchical inheritance
Example:

Output:

output hierarical inheritance


4. extends keyword:

In Java, extends is a keyword and it shows the relation of one class to another. If class A extends class B, then its mean class A inherits class B. extends keyword is used with derived/child class.


5. super Keyword:

super keyword is used to invoke the constructor of super/parent class in the child class. It is also used to differentiate the members of parent class from the members of child class if they have same names.

Example:

Output:

Output using super keyword
Output using super keyword

6. This Keyword:

this keyword is used to invoke the constructor of child class in the child class; It is also used to differentiate the members of parent class from the members of the child class if they have same names.

Example:

 


7. Access Specifiers for Encapsulation:

In Java, there are four Access Levels. These access specifiers are used to hide or show the details of the members of the class, the concept of information hiding is known as Encapsulation.
1. default.
2. public.
3. private.
4. protected.

default:
     The default specifier makes the member visible only inside the package of the class it is declared and initialized.

public:
    The public specifier makes the member visible all around the project.

private:
 The private specifier makes the member visible only inside the class it is declared and initialized.

protected:
     The protected specifier makes the member visible inside the class it is declared and initialized along with the derived or child class only.

 

In this tutorial, you have learned the concept of inheritance in object-oriented programming along with along with the use of this and super keywords in Java. In the next tutorial, you will learn the concepts of overloading and overriding in Java.

Hope that you like this tutorial, Stay tuned for more tutorials. Stay Blessed!