Types:
- Single Inheritance (using extends keyword)
- Multilevel Inheritance (grand parent-->parent-->child)
- Hierarchical Inheritance (single parent multiple child)
- Multiple Inheritance (Not supported in java)
- Hybrid Inheritance (combination of all other inheritance)
Java supports Single, Multilevel, and Hierarchical inheritance using classes. Multiple and Hybrid inheritance are not supported using classes but can be achieved using interfaces.
parent
/ \
Dog Cat
Multilevel Inheritance
- A class inherits from a class, and another class inherits from that class. Example: class Animal { void eat() { System.out.println("Animal eats"); } }
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
Multiple Inheritance:
- it will not supported in java. because of ambiquity issue will come.
- One child class inherits from multiple parent classes.
Top comments (0)