- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
1. Single Inheritance
- In single inheritance, a single subclass extends from a single superclass. For example,
Example:
class Animal{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
barking…
eating…
2. Multilevel Inheritance
- In multilevel inheritance, a subclass extends from a superclass and then the same subclass acts as a superclass for another class. For example,
Example:
class Animal{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
weeping…
barking…
eating…
3. Hierarchical Inheritance
- In hierarchical inheritance, multiple subclasses extend from a single superclass. For example,
4. Multiple Inheritance
- In multiple inheritance, a single subclass extends from multiple superclasses. For example,
Note: Java doesn’t support multiple inheritance. However, we can achieve multiple inheritance using interfaces [TBD]
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. Here, we have combined hierarchical and multiple inheritance to form a hybrid inheritance.
Difference Between this and this() in Java
- this keyword is used to refer to the current object, i.e. through which the method is called.
- this() is used to call one constructor from the other of the same class.
super Keyword
- The super keyword in java is a reference variable that is used to refer parent class objects.
https://www.programiz.com/java-programming/inheritance
https://www.softwaretestinghelp.com/types-of-inheritance-in-java/
https://www.geeksforgeeks.org/difference-between-this-and-this-in-java/
https://www.javatpoint.com/this-vs-super-in-java
https://www.geeksforgeeks.org/difference-between-super-and-super-in-java-with-examples/
Leave a comment