Inheritance:
class is allowed to inherit the features(fields and methods) of another class.A class that inherits from another class can reuse the methods and fields of that class.
Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class).
Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
Extends Keyword: This keyword is used to inherit properties from a superclass.
Types of inheritance:
1.Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance.
2.Multilevel Inheritance:
In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also acts as the base class for other classes.
3.Hierarchical Inheritance:
In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class. For example, cars and buses both are vehicle
4.Multiple Inheritance (Through Interfaces)
In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes.
Note: that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through Interfaces.
5.Hybrid Inheritance:
It is a mix of two or more of the above types of inheritance. In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve multiple inheritance to implement Hybrid inheritance.
Top comments (0)