Select Page

Inheritance in Java – What is it?

Inheritance in Java is a process where by one class acquires the fields and methods of another class.

nanadwumor

May 27, 2023
Inheritance in Java

Share this article

Inheritance in Java is a process where by one class acquires the fields and methods of another class.

Inheritance is one of the pillars of object-oriented programming(OOP). The others are Abstraction, Polymorphism, and Encapsulation.

The purpose of inheritance in OOP is to make a written code reusable such that a new class has to write only the unique features and rest of the common fields and methods can be inherited from the older class.


Join other Subscribers on our YouTube channel and enjoy daily pogramming tutorials.


The Superclass and subclass

If we write class A and later decide to add some functions to class A, there’s no need to rewrite the entire class from scratch if we are just tweaking the functionality of class A.

We can create a new class B with the new features, then let class B inherit the old features of class A instead of rewriting those old features and adding to class B.  

In Java technical terms, we say class B extends class A. That’s, it extends the functionality of class A by adding additional code.

Recommended



The class A is called the Superclass while class B is called the subclass. Alternatively, you can call class A the Parent class and class B, the child class. Some people also call class A the base class and class B, the derived class.

Syntax of Inheritance

The “is a” relationship in inheritance

Inheritance creates what we call “is a” relationship among Classes.

Example, Let’s assume we have a class called Athlete that defines an athlete. We can also create other classes like Footballer class, BaseballPlayer class, CricketPlayer class, VolleyballPlayer class which are all different types of athletes. In other words, they are all subclasses of the superclass Athlete.

A baseball player “is a” type of athlete

A volleyball player “is a” type of athlete

A cricket player “is a” type of athlete

A footballer “is a” type of athlete

That’s, the subclassis a” specialized type of the superclass. This is why we say inheritance always create “is a” relationship between the superclass and the subclass.

Example, in the below program, StudentExam class is used to grade students. A new class called EntranceExam will extend StudentExam in subsequent code.

Program demonstrates use of the StudentExam class to grade a student

A new class called EntranceExam extends StudentExam

Let’s assume that after using StudentExam class for sometime, we decide to organize another exam called entrance exam for students. Let’s write a class called EntranceExam to grade the new exam.

We decide that we’ll take the number of questions students answer, the number of questions they get correct or miss and points they gain into consideration. 

After all these consideration, we’ll still grade the student with a letter grade so instead of adding that functionality to our new class Called EntranceExam, we decide to inherit the code we already have in the StudentExam class. That’s, we reuse that code. 

To do that, we let our new class EntranceExam extend StudentExam class. This gives EntranceExam the opportunity to inherit all the members of StudentExam class.

The ‘extends’ keyword

We see the word extends used by the EntranceExam class. This means EntranceExam class inherits all the members (fields and methods) of the StudentExam class.

Private members in inheritance

In Java, we have access modifiers. These are private, public, protected and default. 

When EntranceExam class extends StudentExam class, EntranceExam class inherits all members of the StudentExam class and can access all except members that are declared private to the StudentExam class.

Thus, EntranceExam class cannot access the private field called score, inherited from StudentExam class. To access this private field, EntranceExam objects will have to use the public method called getScore() which has direct access to this private field because it is part of the StudentExam class.

Fields and Methods native and those  inherited

Let’s take a closer look at the EntranceExam class now. What fields does it have now and which of them can it access?  

What methods does it have now and which of them can it access?

Fields of the EntranceExam class

int TotalQuest  –  native to EntranceExam class

double SinglePoint  –  native to EntranceExam

int TotalCorrect  –  native to EntranceExam

Methods of the EntranceExam class

Constructor  –  native to EntranceExam class

getPointPerQuestion –  native to EntranceExam

getTotalCorrect  –  native to EntranceExam


 setScore  –  inherited from StudentExam

getScore  –  inherited from StudentExam

setGrade  –  inherited from StudentExam

The private members of the superclass

Technically, a subclass inherits all the members of the Superclass. That’s all the fields and methods. 

But the private members of a superclass are literally private to the Superclass. That’s, the subclass cannot access them like the others although it can still access these private fields through a public method that can access the private field.

This means, the StudentExam class private field called score cannot be inherited by any object of the subclass. However, the public method called getScore() returns the score so it can be accessed through the method. 

The other member of StudentExam that cannot be inherited by the EntranceExam subclass is the constructor of the StudentExam class. It makes a lot of sense that a superclass’s constructor cannot be inherited by its subclass though the constructor is itself a type of method.

The sole purpose of a constructor is to construct. That’s, to construct objects of its class.

So, the question is, of what importance is a constructor of a Superclass to a subclass if the constructor cannot be used by the subclass to create an object of itself?

Besides, every subclass has a constructor of its own which is sufficient to create objects of its kind.

Program demonstrates EntranceExam class

Inheritance does not work the opposite way

A subclass extends the functionality of a Superclass. In other words, a subclass inherits the fields and members of a Superclass. 

However, a Superclass cannot inherit or access any of the members of a subclass.

A real life metaphor of inheritance

Inheritance in Java is quite similar to inheritance in real life. It’s the norm for a child to inherit his parents assets but the opposite rarely happens. 

An object of StudentExam cannot call these methods : getPoint, getTotalCorrect. This is because they belong to a subclass. A Superclass cannot inherit from a subclass. 

The Superclass can call only its methods and fields. 

So in summary, a subclass inherits all members of Superclass that aren’t private. However, a Superclass cannot inherit any member of the subclass.


Share this article


You May Also Like…

double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal...

float data type in Java
float data type in Java

It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of...