Java Topics

Inheritence

The process by which one class acquires the properties ( data member/ variables) and functionalities ( methods) of another class is called "inheritance".

  • Child class/ subclass/ derived class always implements/ extends the properties of parent/ super/ base class.
  • Static members of the parent class cannot be inherited to child class i.e. We can access static members but we can't inherit.
  • Static members of the superclass can be accessed from the subclass object.
  • Private members of the superclass cannot be inherited to subclass.
  • Superclass constructor cannot be inherited to subclass.

Different types of inheritance:

(i) Single inheritance:

                One subclass extends the properties of one super class.

(ii) Multilevel inheritance:

                Subclass inheriting from superclass again that super class inheriting from another super class is called multilevel inheritance

(iii) Hierarchical inheritance:

                 Two or more subclasses inheriting the properties from one super class is called hierarchical inheritance.

(iv) Multiple inheritance:

                One sub class extends more than one superclass.

Why Java does not support multiple inheritance?

Reason is,

1.To call constructors of both the superclasses, we should write to super() statements which is not supported.

2. If both the super classes have the method with the same name and the same parameters, it will lead to confusion that which method should be executed from the subclass object.

(v) Hybrid inheritance:

                Combination of two or more different types of inheritance called hybrid inheritance.