Inheritance in Java

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit properties and behaviors from other classes. In Java, inheritance provides a powerful mechanism for code reuse, promotes modularity, and enables the creation of class hierarchies. In this article, we will explore the syntax and usage of inheritance in Java, along with a practical example.

Syntax of Inheritance

In Java, inheritance is implemented using the extends keyword. The syntax for creating a subclass that inherits from a superclass is as follows:

    
class Subclass extends Superclass {
    // Subclass members
}

Single Inheritance

Single inheritance occurs when a subclass inherits from a single superclass. It is the most basic form of inheritance in Java. Here's the syntax for single inheritance:

    
class Subclass extends Superclass {
    // Subclass members
}

Example
    
// Superclass
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }
}

// Subclass
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

// Creating an object of the subclass
Dog dog = new Dog();

// Accessing superclass and subclass members
dog.eat();   // Output: Animal is eating.
dog.bark();  // Output: Dog is barking.

Multilevel Inheritance

Multilevel inheritance occurs when a subclass becomes the superclass for another subclass, forming a hierarchy. Here's the syntax for multilevel inheritance.


class Subclass1 extends Superclass {
    // Subclass1 members
}

class Subclass2 extends Subclass1 {
    // Subclass2 members
}

Example

// Superclass
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }
}

// Subclass1
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

// Subclass2
class GermanShepherd extends Dog {
    void guard() {
        System.out.println("German Shepherd is guarding.");
    }
}

// Creating an object of the subclass2
GermanShepherd gs = new GermanShepherd();

// Accessing superclass, subclass1, and subclass2 members
gs.eat();    // Output: Animal is eating.
gs.bark();   // Output: Dog is barking.
gs.guard();  // Output: German Shepherd is guarding.

Hierarchical Inheritance

Hierarchical inheritance occurs when multiple subclasses inherit from a single superclass. Here's the syntax for hierarchical inheritance.

    
class Subclass1 extends Superclass {
    // Subclass1 members
}

class Subclass2 extends Superclass {
    // Subclass2 members
}

Example
    
// Superclass
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }
}

// Subclass1
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

// Subclass2
class Cat extends Animal {
    void meow() {
        System.out.println("Cat is meowing.");
    }
}

// Creating objects of the subclasses
Dog dog = new Dog();
Cat cat = new Cat();

// Accessing superclass, subclass1, and subclass2 members
dog.eat();   // Output: Animal is eating.
dog.bark();  // Output: Dog is barking.

cat.eat();   // Output: Animal is eating.
cat.meow();  // Output: Cat is meowing.

Multiple Inheritance (via Interfaces)

Java doesn't support multiple inheritance of classes, but it allows a class to implement multiple interfaces. This achieves a form of multiple inheritance. Here's the syntax for implementing interfaces:

    
class ClassName implements Interface1, Interface2 {
    // Class members
}

Example
    
// Interfaces
interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

// Class implementing multiple interfaces
class Duck implements Flyable, Swimmable {
    @Override
    public void fly() {
        System.out.println("Duck is flying.");
    }

    @Override
    public void swim() {
        System.out.println("Duck is swimming.");
    }
}

// Creating an object of the class
Duck duck = new Duck();

// Accessing interface methods
duck.fly();   // Output: Duck is flying.
duck.swim();  // Output: Duck is swimming.

Note

Multiple inheritance is not directly supported in Java because it can lead to certain complexities and conflicts known as the "Diamond Problem." The Diamond Problem occurs when a class inherits from two or more classes that have a common superclass. This situation creates ambiguity when resolving method or attribute conflicts between the parent classes.

To avoid such conflicts, Java chooses to support single inheritance (i.e., a class can only inherit from one superclass) while providing a different mechanism for achieving some aspects of multiple inheritance through interfaces. Interfaces allow a class to implement multiple interfaces, each defining a set of methods that the implementing class must implement. This approach ensures that there are no conflicts between inherited methods or attributes.