notes oh inheritance
notes oh inheritance
properties (fields and methods) of another class. There are several types
of inheritance in Java, each with its own characteristics. Below are the
types of inheritance, along with diagrams and example code.
1. Single Inheritance
Superclass
|
Subclass
Code:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
2. Multilevel Inheritance
3. Hierarchical Inheritance
Superclass1 Superclass2
\ /
Subclass
Workaround: Use interfaces to achieve multiple inheritance.
Code:
java
Copy
interface Animal {
void eat();
}
interface Pet {
void play();
}
5. Hybrid Inheritance
Supported in
Type Description
Java?
Single
Yes One subclass inherits from one superclass.
Inheritance
Multilevel Yes A chain of inheritance (e.g., A → B → C).
Multiple subclasses inherit from one
Hierarchical Yes
superclass.
Multiple No (with One subclass inherits from multiple
Supported in
Type Description
Java?
classes) superclasses (achieved via interfaces).
Hybrid No (directly) A combination of inheritance types.