What is inheritance and what are its types?

Technology: java OOPs
AskedIn:
Topics:
Type:

1. It is one of the pillars of OOP(Object Oriented Programming)

2. It is a mechanism in which one class is allowed to inherit/re-use the features (fields and methods) of another class

3. The “extend” keyword is used to inherit any class

4. Inheritance helps in code reusability, method overriding, abstraction

5. Object is a default super-class of every class. In the absence of any other explicit super-class, every class is implicitly a sub-class of Object class

6. A sub-class inherits all the members (fields, methods, nested classes) from its super-class except Constructors of super-class because they are not class members but they can be invoked from sub-class

7. A sub-class does not inherit the private members of its parent class. However, if the superclass has public or protected methods (like getters and setters) for accessing its private fileds, these can also be used by subclass

 

Types of Inheritance

  1. Single Inheritance: When a sub-class inherits the features of only one super-class
  2. Multilevel Inheritance: When a derived class inherits the features of base class and the derived class also acts a base class for other classes
  3. Hierarchical Inheritance: When multiple classes inherits the features of a same base class
  4. Multiple Inheritance:
    1. Multiple inheritance is achieved through interfaces in java
    2. In this type of inheritance one base-class can have multiple super-classes