What is Inheritance?
Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs.
Types of Inheritance
There are Various types of inheritance in Java:
- Single Inheritance:
- Multiple Inheritance:
- Multilevel Inheritance:
In Single Inheritance one class extends another class (one class only).
In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.
In Multiple Inheritance, one class extending more than one class. Java does not support multiple inheritance.
As per above diagram, Class C extends Class A and Class B both.
In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.
As per shown in diagram Class C is subclass of B and B is a of subclass Class A.
0 Comments
If you have any doubts,please let me know