What Inheritance in Java and How It can Help You

Inheritance is a process in which a new class is derived from an existing class. In Java, classes may inherit or acquire the properties (variables and methods) and behaviors from its predecessors or parent class.

In other words,

A subclass inherits the characteristics (properties and methods) of its superclass (ancestor).

Let us dig in deeper to get insights into Inheritance in java.

Inheritance is a robust armament of Java that helps make it an extensively adequate language. It helps decrease code repetition and also reduces on the bugs. If you write the code in the parent class, you no longer need to write the same code for multiple child classes that have the same properties.

Java inheritance lets you have an efficient way to recognize the interaction between the Objects to re-use your code and ensure that you don’t need to retype the same stuff continuously. It allows you to define relationships among objects, think about some examples of Objects that DO have one or more relationships.

Let’s take an example of the object Vehicle that is a common term for a car, bus, and motorcycle.

Have you ever thought about how a car is a vehicle, a motorcycle is a vehicle, and a bus is a vehicle? Do you ever see it? I don’t think you ever did.

This is what the relationship Java Inheritance is all about. In general, when you say something ‘is a’ something else, you really have a connection between those two objects, and therefore you have Inheritance.

How Inheritance helps you

Given the examples above, it means that a Car/motorcycle/truck inherits behaviors and attributes from a Vehicle.

Coming to a second aspect, what exactly a car is. It’s a vehicle having doors, wheels and about five seats.

What is a bus? It is also a Vehicle perhaps more than four wheels, doors, and almost 30 seats. In the case of a motorcycle, it’s a vehicle with two wheels, one or two seats, but no doors.

When it comes to mapping out every one of the characteristics of the Objects, you usually start to observe the similarities they may have and dissimilarities. This is very crucial when it comes to Java Inheritance. If your Objects share something in common, this can be considered an attribute of the superclass. If the objects don’t share similarities, this shall be attributes of the child classes.

In the above example, the Vehicle is the superclass whereas the car, bus and motorcycle are the child class. A superclass is an object that maintains each of the properties which are common.

To sum up, to more about Inheritance in java, it’s advisable to join an online coaching class. 

How about creating your own
Poll, Survey or Quiz?

CREATE NEW