Inheritance

Inheritance: Extending the properties of one class into another is known as inheritance.

Type of Inheritance:

                There are three types of Inheritance in Java language as given below.
Single inheritance 
(super class)        A

                                ↓

(sub class)           B

A class from which properties are inherited is known as super class and the class in which the properties are inherited is known as sub class.

When one class inherits properties from other class then such type of inheritance is known as single inheritance.

In the above diagram class A is a super class and class B is sub class of super class A and therefore class B contains all the properties of class A as well as properties of its own class.

  1. Multilevel Inheritance

When one class inherits properties from another class and another class inherits properties from another class and so on then such type of inheritance is known as multilevel inheritance.
In the above diagram, class C is sub class of super class B and class B is sub class of super class A hence class C contains all the properties of super classes.

                                                                           A

                                                                           ↓

                                                                           B

                                                                           ↓

                                                                          C

  1. Hierarchical Inheritance:-

When multiple classes extends properties from single class then such type of inheritance is known as hierarchical inheritance.

In the above diagram class B, C and D are sub classes of super class A therefore all the subclasses contain properties of super classes and the properties of their own class.

                                   A

                                    ↓

                                  ↔

↓                                 ↓                                  ↓

B                                 C                                 D

 

 

extends keyword

                extends is standard inbuilt keyword of Java language, it is used to extends the properties of super class in sub class. Whenever any subclass extends the properties of super class then subclass will get all the properties of super class except private properties.

Leave a Reply

Your email address will not be published. Required fields are marked *