Inheritance in Java

Inheritance is the one of the main feature of OOP (Object Oriented Programming). In Inhertiance one class aquires(inherits) the properties and behaviours(methods) of the other class.
The idea behind the inheritance is to provide reusability of code, so for the inherited class, only new methods and new properties can be written, because the inherited new class can reuse the new methods and fields of the old class. this reduces time and number of lines in the program
Types of classes in Inheritance
- Class
- Sub Class/ Child Class / Derived Class
- Super Class/ Parent Class/ Base class
Class: Class is a block of code or template which is used to create objects,class dosent occupy memory when it is created
Sub Class: The class which is inherited (extended) from the another class, this sub-class has all methods and fields of the another class
Super class: The class where sub class gets inherited ,all the methods and fields of the superclass
Syntax for Inheritance in Java
To use the inheritance feature in java, you should use extends keyword to extend functionally and create a new chld class from the parent class
syntax
in the syntax cclass is a child class or new class,where its functionalities are extended from the pclass (parent class)
Example
Output
In The above example, boy class has two fields name and gender, a new class called employee is inherited from the boy class, in the inherited employee class there is no need to redeclare the fields for name and gender because parent class(boy class) already has it, now empno field is created only for the use of the employee class
Types of inheritance
- Single Inheritance
- Multilevel Inheritance
- Hierarchical inheritance
- Hybrid Inheritance
- Multiple Inheritance
Single Inheritance
When one class inherits the another class it is called single inheritance. check the diagaram below class B inherits from class A

Example
Output
Multilevel Inheritance
In Multilevel Inheritance, a new class will be derived from the child class, this new class has methods and fields of the child class and its parent class. lets look into the example for better understating

Example
Output
Hierarchical Inheritance

In Hierarchical Inheritance two or more classes are inherited from one class, or if a parent class has more than 2 child classes then it is called Hierarchical Inheritance
Output
Hybrid Inheritance
Hybrid Inheritance is a combination of the above three inheritance types , Single Inheritance, Multilevel Inheritance, Hierarchical inheritance. using interfaces too you can achieve hybrid inheritance.

Multiple Inheritance
multiple inheritance is not possible in java because of ambiguity, If the two-parent classes have same method name, the compiler can decide which method to be inherited to the child class, so since compile-time errors are safer then run time, java doesn’t allow you to inherit from two-parent classes to a child class, check the example for better understanding

Example (the below program throws an error)