# Inheritance

&#x20;The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object-oriented programming.

#### &#x20;Important points

* &#x20;In the inheritance the class which is give data members and methods is known as base or super or parent class. &#x20;
* The class which is taking the data members and methods is known as sub or derived or child class
* &#x20; The data members and methods of a class are known as features.
* &#x20;The concept of inheritance is also known as re-usability or extendable classes or sub classing or derivation.&#x20;

#### Why use Inheritance?

&#x20;**1**)For Method Overriding (used for Runtime Polymorphism).&#x20;

&#x20;**2**)For code Re-usability

&#x20;**How to use inheritance in Java**&#x20;

To Achieve inheritance between classes, we should use extends keyword.

> &#x20;Note: Only non-static members of a class will be involved in Inheritance

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfYySnMuMreBPH489R%2F-LTfZpG1WEnrMY2SHNPj%2Fimage.png?alt=media\&token=c86af761-c77f-42c1-8914-746f8e822480)

### &#x20;Output

&#x20;Child method\
&#x20;Parent method

#### types of Inheritance

&#x20;1\. Single Inheritance

&#x20;2\. Multilevel Inheritance&#x20;

3\. Hierarchical Inheritance

> &#x20;NOTE: Multiple inheritance is not supported in java

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfYySnMuMreBPH489R%2F-LTf_9GTR5YTIkAHb8BI%2Fimage.png?alt=media\&token=cd0a26d3-b1fe-42c3-beb7-b0e8bdd85b3c)

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfYySnMuMreBPH489R%2F-LTf_GdnAbkxezr2KU_b%2Fimage.png?alt=media\&token=e68c402d-6f5e-401d-b3d0-2f8a66464105)

#### &#x20;Why multiple inheritance is not supported in Java&#x20;

To remove ambiguity. To provide more maintainable and clear design.&#x20;

#### Important Points:

* &#x20;All public variables of Super class will be inherited by subclass.
* &#x20; All default variables will be inherited by all subclasses in the same package only. Subclasses outside the package will not inherit any default member.
* &#x20; Private members cannot be inherited by subclass because they will not be visible to the subclass and hence the subclass can create the Method or property with the same name without any problem.
* &#x20; Protected variables will be inherited by all subclasses in the same package or outside package (Different from default).&#x20;
* &#x20;Methods which are not inherited cannot be overridden. And hence overridden rules cannot be applied to those Methods. But Methods can still be defined in subclass though those Methods will not be the overridden Method. Instead it defines a new Method.&#x20;
* &#x20;Static Methods or variables do not take part in inheritance.
* &#x20; Even though static methods or variables do not take part in inheritance and cannot be overridden they can be redefined in subclass. The redefinition is not called overridden but hidden.

