What is the Inheritance?

Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class. It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

There are five types of inheritance in Java.

  • Single-level inheritance
  • Multi-level inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

In Object Oriented Programming we deal with the real life problems and here inheritance play a vital role in it.Consider a real life situation.

Suppose you have created a class named as “car” and you have included basic functions like gears, acceleration, wheels etc. in it.

Now you want to create another class as “sports car” that consist of all the functions of class car and also contains some new functions such as nitro, map, GPS etc. which will be of its own.

With the help of inheritance you can inherit those functions of car class in sports car class which you desire.We don’t have to create unique functions to this. Thus by doing this we can save a lot of time and some unnecessary programming.