Java Types of Inheritance
Inheritance in Java is a fundamental concept of object-oriented programming that allows a class (called a subclass or derived class) to inherit fields and methods from another class (called a superclass or base class). This promotes code reuse and establishes a hierarchical relationship between classes. There are several types of inheritance in Java, which can be categorized as follows:
1. Single Inheritance
In single inheritance, a class inherits from only one superclass. This is the simplest form of inheritance and ensures that the subclass can directly access the members (fields and methods) of the superclass.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound(); // Inherited method
myDog.bark(); // Dog's own method
}
}
2. Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class, forming a chain of inheritance. A subclass can inherit from a superclass, and then another subclass can inherit from that subclass.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy whimpers");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Puppy myPuppy = new Puppy();
myPuppy.sound(); // Inherited from Animal
myPuppy.bark(); // Inherited from Dog
myPuppy.weep(); // Puppy’s own method
}
}
3. Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from a single superclass. This allows several classes to share the common behavior defined in the superclass while having their unique implementations.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
Cat myCat = new Cat();
myDog.sound(); // Inherited from Animal
myDog.bark(); // Dog’s own method
myCat.sound(); // Inherited from Animal
myCat.meow(); // Cat’s own method
}
}
4. Multiple Inheritance (Through Interfaces)
Java does not support multiple inheritance with classes (to avoid ambiguity), but it allows a class to implement multiple interfaces. This enables a class to inherit behavior from multiple sources.
Example:
interface CanRun {
void run();
}
interface CanBark {
void bark();
}
class Dog implements CanRun, CanBark {
public void run() {
System.out.println("Dog runs");
}
public void bark() {
System.out.println("Dog barks");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.run(); // Dog's implementation of run
myDog.bark(); // Dog's implementation of bark
}
}
5. Hybrid Inheritance (Through Interfaces)
Hybrid inheritance is a combination of two or more types of inheritance. In Java, hybrid inheritance can be achieved by combining different forms of inheritance and using interfaces to avoid issues related to multiple inheritance.
Example:
interface CanSwim {
void swim();
}
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal implements CanSwim {
public void swim() {
System.out.println("Dog swims");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.swim(); // Dog's implementation of swim
}
}
Summary of Inheritance Types:
- Single Inheritance: One class inherits from another.
- Multilevel Inheritance: A class is derived from another derived class, creating a hierarchy.
- Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
- Multiple Inheritance: Achieved through interfaces; a class can implement multiple interfaces.
- Hybrid Inheritance: A combination of multiple inheritance types, generally utilizing interfaces.
Inheritance is a powerful feature in Java that promotes code reusability, establishes relationships between classes, and facilitates polymorphism. By understanding the different types of inheritance, developers can design more flexible and maintainable code.