DEV Community

Dinesh G
Dinesh G

Posted on

Types of Inheritances

Types Of Inheritance

  • Single Level Inheritance
  • Multi Level Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

=> Java does not support multiple or hybrid inheritance through classes because it can lead to the diamond problem, resulting in ambiguity.

Single Level Inheritance

 Multilevel inheritance is a type of inheritance where one child class inherits the properties and behaviors of one parent class. It means there is only one parent and one child class.
Enter fullscreen mode Exit fullscreen mode

Syntax:

class parent_class_name {
    void eat() { }
}

class child_class_name extends parent_class_name {
    void bark() { }
}
Enter fullscreen mode Exit fullscreen mode

Multi level Inheritance

Multilevel inheritance is a type of inheritance where a class inherits from a child class which is already inherited from another class. It means there is a chain of inheritance, one parent, one  child, and one grandchild class.
Enter fullscreen mode Exit fullscreen mode

Syntax:

class parent_class_name {

}

class child_class_name extends parent_class_name {

}

class grand_child_class_name extends child_class_name {

}
Enter fullscreen mode Exit fullscreen mode

Hierarchical inheritance

Hierarchical inheritance is a type of inheritance where a single parent class is inherited by multiple child classes. In this structure, all child classes share the common properties and behaviors of the parent class, but they can also define their unique features.

Syntax:

class parent_class_name {

}

class child_class_name_1 extends parent_class_name {

}

class child_class_name_2 extends parent_class_name {

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)