java inheritance tutorial

java inheritance tutorial in this topic you will understand the concepts of inheritance.Its advantages and disadvantages.

This chapter of bodhak java tutorial explains inheritance along with syntax and example programming. In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance.

 

java inheritance tutorial

Ex: car is a vehicle

the house is a building

a horse is an animal

IS A relationship is established by using keywords extends and implements in the class declaration.

Advantages of Inheritance:

Reusability — facility to use public methods of a base class without rewriting the same.

Extensibility — extending the base class logic as per business logic of the derived class.

Data hiding — base class can decide to keep some data private so that it cannot be altered by the derived class

Minimize the amount of duplicate code in an application: If the duplicate code (variable and methods) exists in two related classes, we can be refactored that hierarchy by moving that common code up to the common super class.

Better organization of code: Moving of common code to super class results in the better organization of code.

Code more flexible change:  Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is a super class.

Overriding: With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class.

Disadvantages of Inheritance:

The inheritance relationship is a, tightly coupled relationship, there will be tight bonding between parent and child.

Increased time/effort it takes the program to jump through all the levels of overloaded classes.

If a method signature is changed then we will be affected in both cases (inheritance & composition)