Inheritance in Java

Yogeshwar
3 min readFeb 24, 2021

In the previous article, we have discussed Encapsulation, in this article we will learn Inheritance in Java.

As the name suggests, inheritance means to take something that already exists.

Inheritance is an important feature of OOP (Object-oriented Programming). It is the mechanism that one class is allowed to inherit all the features of another class.

Inheritance is the process of developing a new class/object upon the functionality of an existing object to add a more specialized functionality.

Important terminologies in Inheritance

  1. Super Class: Superclass is a class from which we inherit all the properties. It is also known by different names like base class, parent class.
  2. Sub Class: A subclass is a class that inherits another class. It is also known as derived class. It is also known by different names like child class, derived class, subclass. A subclass is having its own properties and it also inherits from the parent class.
Inheritance

In the above diagram, Vehicle is a parent class which is having its own properties and behaviour, when the Car class extends the Vehicle class then the Car class has its own properties and behaviour and it gets all public properties and behaviour from the parent class also i.e. Vehicle.

Why should we develop inheritance?

When we find an object with the same properties and behaviour as an existing object, we must develop an inheritance between these two objects. So that new object contains both properties and behaviour of a current object and existing object.

Advantages of Inheritance

  1. Reusability:- you can reuse the fields and methods of the existing class when you create a new class.
  2. Runtime Polymorphism:- It is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

How to implement Inheritance in Java?

Inheritance can be implemented in java by using two keywords,

  1. extends
  2. implements

extends” is used when we develop inheritance between two classes or two interfaces.

implements” is used when we develop inheritance between interface and class.

How to access static and non-static members from the super class?

  1. We can access static members directly by their name or by using the subclass name.
  2. We can access non-static members by using a subclass object.

The syntax of Java Inheritance,

Parent Class
Inheritance using the “extends” keyword
Parent Interface
Inheritance using the “implements” keyword

So this is the basics about Inheritance, in the next article, we will see each Type of Inheritance with examples in detail.

Keep in touch, Sharing is Caring!

Thank you…

--

--

Yogeshwar

Hi, I am Yogeshwar. I am a Software developer by Profession and I am here to share knowledge with you guys.