java

Java Inheritance


What is Inheritance?

Inheritance is a fundamental concept that allows a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class).

Note: The main purpose of using inheritance is, it provide us  code reusability concept and establish a relationship between classes.

Syntax

class Animal {
    void eat() {
       // code;
    }
}

class Dog extends Animal {
    void bark() {
    }
}
class A {
    public static void main(String[] args) {
        // create object and do other stuffs here
    }
}

 

Example

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

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

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

// Main class
public class InheritanceExample {
    public static void main(String[] args) {
        // Creating an instance of the subclass
        Dog myDog = new Dog();

        // Accessing methods from the superclass
        myDog.eat();
        myDog.sleep();

        // Accessing method from the subclass
        myDog.bark();
    }
}

 

Types of Inheritance

 

1. Single Inheritance

In single inheritance, a class can extend only one superclass. Java supports single inheritance to avoid the complexities and ambiguities that can arise with multiple inheritance.

Syntax

class A {
    // ...
}

class B extends A {
    // ...
}

Example

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

public class SingleInheritanceDemo {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.eat();
        myDog.bark();
    }
}

 

2. Multiple Inheritance:

Although Java does not support multiple inheritance for classes, it allows a class to implement multiple interfaces. This is a form of multiple inheritance through interfaces.

Syntax

interface A {
    // ...
}

interface B {
    // ...
}

class C implements A, B {
    // ...
}

Example

interface Walks {
    void walk();
}

interface Swims {
    void swim();
}

class Fish implements Walks, Swims {
    @Override
    public void walk() {
        System.out.println("Fish can walk on fins.");
    }

    @Override
    public void swim() {
        System.out.println("Fish can swim in water.");
    }
}

public class MultipleInheritanceDemo {
    public static void main(String[] args) {
        Fish myFish = new Fish();
        myFish.walk();
        myFish.swim();
    }
}

 

3. Multilevel Inheritance:

In multilevel inheritance, a class can inherit from another class, and then another class can inherit from it. This forms a chain of inheritance.

Syntax

class A {
    // ...
}

class B extends A {
    // ...
}

class C extends B {
    // ...
}

Example

class Bird {
    void fly() {
        System.out.println("This bird can fly.");
    }
}

class Sparrow extends Bird {
    void chirp() {
        System.out.println("The sparrow chirps.");
    }
}

class FlyingSparrow extends Sparrow {
    void flyHigh() {
        System.out.println("The sparrow flies higher.");
    }
}

public class MultilevelInheritanceDemo {
    public static void main(String[] args) {
        FlyingSparrow mySparrow = new FlyingSparrow();
        mySparrow.fly();
        mySparrow.chirp();
        mySparrow.flyHigh();
    }
}

 

4. Hierarchical Inheritance:

In hierarchical inheritance, multiple classes inherit from a single superclass. Each subclass shares common features with the superclass but may have additional or overridden functionality.

Syntax

class Animal {
    // ...
}

class Dog extends Animal {
    // ...
}

class Cat extends Animal {
    // ...
}

Example

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("The cat meows.");
    }
}

public class HierarchicalInheritanceDemo {
    public static void main(String[] args) {
        Cat myCat = new Cat();
        myCat.eat();
        myCat.meow();
    }
}

 

5. Hybrid Inheritance:

Hybrid inheritance is a combination of two or more types of inheritance mentioned above. For example, a class might inherit from another class and implement multiple interfaces.

Syntax

interface A {
    // ...
}

interface B {
    // ...
}

class C extends A implements B {
    // ...
}

Example

// Base class for single inheritance
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Class implementing interfaces for multiple inheritance
interface Walks {
    void walk();
}

interface Swims {
    void swim();
}

class Fish implements Walks, Swims {
    @Override
    public void walk() {
        System.out.println("Fish can walk on fins.");
    }

    @Override
    public void swim() {
        System.out.println("Fish can swim in water.");
    }
}

// Class for multilevel inheritance
class Bird extends Animal {
    void fly() {
        System.out.println("This bird can fly.");
    }
}

class Sparrow extends Bird {
    void chirp() {
        System.out.println("The sparrow chirps.");
    }
}

class FlyingSparrow extends Sparrow {
    void flyHigh() {
        System.out.println("The sparrow flies higher.");
    }
}

// Hybrid inheritance combining single, multiple, and multilevel
class HybridExample extends FlyingSparrow implements Walks {
    void hybridFunction() {
        System.out.println("This is a function of the hybrid class.");
    }
}

public class HybridInheritanceDemo {
    public static void main(String[] args) {
        HybridExample hybridObject = new HybridExample();

        hybridObject.eat();        // From Animal (Single Inheritance)
        hybridObject.fly();        // From Bird (Multilevel Inheritance)
        hybridObject.chirp();      // From Sparrow (Multilevel Inheritance)
        hybridObject.flyHigh();    // From FlyingSparrow (Multilevel Inheritance)
        hybridObject.walk();       // From Walks (Multiple Inheritance)
        hybridObject.hybridFunction();  // From HybridExample (Single + Multiple + Multilevel)
    }
}