types of inheritance in java
In Java, inheritance can be classified into different types based on the relationships between classes. The main types of inheritance are:
1. Single Inheritance:
2. Multilevel Inheritance:
3. Hierarchical Inheritance:
4. Hybrid Inheritance:
5. Multiple Inheritance (through Interfaces):
1. Single Inheritance:
In single inheritance, a class can extend only one superclass. Java supports single inheritance for classes, meaning a class can have only one direct parent class.
// Parent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}
// Child class (subclass) inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Dog class
Dog myDog = new Dog();
// Accessing methods from both Animal and Dog classes
myDog.eat(); // Inherited from Animal
myDog.sleep(); // Inherited from Animal
myDog.bark(); // Specific to Dog
}
}
In this example:
Animal
is the parent class (superclass) with methodseat()
andsleep()
.Dog
is the child class (subclass) that extendsAnimal
. It inherits the methodseat()
andsleep()
from theAnimal
class and adds its own methodbark()
.- The
main
method demonstrates creating an instance of theDog
class and calling methods from both theAnimal
andDog
classes.
This represents a simple example of single inheritance, where a subclass extends a single superclass.
2. Multilevel Inheritance:
In multilevel inheritance, a class serves as a superclass for another class, and that subclass, in turn, becomes the superclass for another class.
In this example, C
inherits from B
, which, in turn, inherits from A
.
// Grandparent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}
// Parent class (subclass) inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
// Child class (subclass) inheriting from Dog
class Bulldog extends Dog {
void guard() {
System.out.println("Bulldog is guarding.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Bulldog class
Bulldog myBulldog = new Bulldog();
// Accessing methods from Animal, Dog, and Bulldog classes
myBulldog.eat(); // Inherited from Animal
myBulldog.sleep(); // Inherited from Animal
myBulldog.bark(); // Inherited from Dog
myBulldog.guard(); // Specific to Bulldog
}
}
In this example:
Animal
is the grandparent class (superclass) with methodseat()
andsleep()
.Dog
is the parent class (subclass) that extendsAnimal
. It inherits the methodseat()
andsleep()
from theAnimal
class and adds its own methodbark()
.Bulldog
is the child class (subclass) that extendsDog
. It inherits the methodseat()
,sleep()
, andbark()
from theDog
class and adds its own methodguard()
.- The
main
method demonstrates creating an instance of theBulldog
class and calling methods from theAnimal
,Dog
, andBulldog
classes.
This represents a multilevel inheritance scenario where a class extends another class, which in turn is extended by another class.
3. Hierarchical Inheritance:
In hierarchical inheritance, multiple classes inherit from a common superclass. It forms a tree-like structure.
Both B
and C
inherit from a common superclass A
.
// Parent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}
// Child class 1 (subclass) inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
// Child class 2 (subclass) inheriting from Animal
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Dog class
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.sleep(); // Inherited from Animal
myDog.bark(); // Specific to Dog
System.out.println(); // Separating output for clarity
// Creating an instance of the Cat class
Cat myCat = new Cat();
myCat.eat(); // Inherited from Animal
myCat.sleep(); // Inherited from Animal
myCat.meow(); // Specific to Cat
}
}
In this example:
Animal
is the parent class (superclass) with methodseat()
andsleep()
.Dog
is one child class (subclass) that extendsAnimal
. It inherits the methodseat()
andsleep()
from theAnimal
class and adds its own methodbark()
.Cat
is another child class (subclass) that extendsAnimal
. It inherits the methodseat()
andsleep()
from theAnimal
class and adds its own methodmeow()
.- The
main
method demonstrates creating instances of both theDog
andCat
classes and calling methods from theAnimal
,Dog
, andCat
classes.
This represents a hierarchical inheritance scenario where multiple classes inherit from a common superclass.
4. Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance mentioned above. For example, a class might exhibit multiple inheritance through interfaces, and within those interfaces, it might have multilevel or hierarchical inheritance.
// Interface with a method
interface Walkable {
void walk();
}
// Grandparent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}
// Parent class (subclass) inheriting from Animal and implementing Walkable interface
class Dog extends Animal implements Walkable {
void bark() {
System.out.println("Dog is barking.");
}
// Implementing the walk() method from the Walkable interface
@Override
public void walk() {
System.out.println("Dog is walking.");
}
}
// Parent class (subclass) inheriting from Animal and implementing Walkable interface
class Cat extends Animal implements Walkable {
void meow() {
System.out.println("Cat is meowing.");
}
// Implementing the walk() method from the Walkable interface
@Override
public void walk() {
System.out.println("Cat is walking.");
}
}
// Child class (subclass) inheriting from Dog
class Bulldog extends Dog {
void guard() {
System.out.println("Bulldog is guarding.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Bulldog class
Bulldog myBulldog = new Bulldog();
// Accessing methods from Animal, Dog, and Bulldog classes
myBulldog.eat(); // Inherited from Animal
myBulldog.sleep(); // Inherited from Animal
myBulldog.bark(); // Inherited from Dog
myBulldog.walk(); // Implemented from Walkable interface in Dog
myBulldog.guard(); // Specific to Bulldog
}
}
In this example:
Animal
is the grandparent class (superclass) with methodseat()
andsleep()
.Dog
andCat
are parent classes (subclasses) that extendAnimal
and implement theWalkable
interface.Bulldog
is a child class (subclass) that extendsDog
.- The
Walkable
interface defines awalk()
method that bothDog
andCat
implement. - The
main
method demonstrates creating an instance of theBulldog
class and calling methods from theAnimal
,Dog
,Bulldog
, and theWalkable
interface.
This example illustrates a combination of single inheritance, interface-based multiple inheritance, and hierarchical inheritance, making it a form of hybrid inheritance.
5. Multiple Inheritance (through Interfaces):
Java does not support multiple inheritance for classes, meaning a class cannot directly extend more than one class. However, multiple inheritance can be achieved through interfaces. Here’s an example demonstrating multiple inheritance through interfaces:
// Interface with a method
interface Walkable {
void walk();
}
// Interface with a method
interface Swimmable {
void swim();
}
// Parent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}
// Child class (subclass) inheriting from Animal and implementing Walkable and Swimmable interfaces
class Dolphin extends Animal implements Walkable, Swimmable {
// Implementing the walk() method from the Walkable interface
@Override
public void walk() {
System.out.println("Dolphin is walking on its flippers.");
}
// Implementing the swim() method from the Swimmable interface
@Override
public void swim() {
System.out.println("Dolphin is swimming gracefully.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Dolphin class
Dolphin myDolphin = new Dolphin();
// Accessing methods from Animal, Dolphin, Walkable, and Swimmable
myDolphin.eat(); // Inherited from Animal
myDolphin.sleep(); // Inherited from Animal
myDolphin.walk(); // Implemented from Walkable interface in Dolphin
myDolphin.swim(); // Implemented from Swimmable interface in Dolphin
}
}
In this example:
Animal
is the parent class (superclass) with methodseat()
andsleep()
.Walkable
andSwimmable
are interfaces with a single method each:walk()
andswim()
.Dolphin
is a child class (subclass) that extendsAnimal
and implements bothWalkable
andSwimmable
interfaces.- The
main
method demonstrates creating an instance of theDolphin
class and calling methods from theAnimal
,Dolphin
,Walkable
, andSwimmable
interfaces.
This illustrates multiple inheritance through interfaces in Java. The Dolphin
class inherits from both the Animal
class and the Walkable
and Swimmable
interfaces.
These types of inheritance provide flexibility in designing class hierarchies, and each has its own set of advantages and considerations. Java’s support for single inheritance for classes helps prevent some of the complexities associated with multiple inheritance, while interfaces provide a way to achieve multiple inheritance-like behavior.