Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Inheritance in Java
Syntax and Examples



Inheritance allows a class to inherit fields and methods from another class, promoting code reuse and logical hierarchy. In simple terms, inheritance lets us create new classes based on existing ones.

Why Use Inheritance?

Types of Inheritance in Java

Java supports the following types of inheritance:

Note: Java does not support multiple inheritance with classes to avoid ambiguity. However, it can be achieved using interfaces.

Syntax of Inheritance

Java uses the extends keyword to establish inheritance between classes:

class Parent {
    // fields and methods
}

class Child extends Parent {
    // additional fields and methods
}

Example 1: Single Inheritance

Let’s understand with a simple example:

class Animal {
    void sound() {
        System.out.println("Animals make sounds");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();  // inherited method
        d.bark();   // own method
    }
}
Animals make sounds
Dog barks

Explanation: The class Dog inherits the sound() method from the Animal class using extends. This demonstrates how a subclass reuses parent functionality.

Example 2: Multilevel Inheritance

class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

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

class Puppy extends Dog {
    void weep() {
        System.out.println("Weeping...");
    }
}

public class Main {
    public static void main(String[] args) {
        Puppy p = new Puppy();
        p.eat();
        p.bark();
        p.weep();
    }
}
Eating...
Barking...
Weeping...

Explanation: The Puppy class inherits from Dog, which in turn inherits from Animal. This chain forms a multilevel hierarchy.

Example 3: Hierarchical Inheritance

class Animal {
    void breathe() {
        System.out.println("Breathing...");
    }
}

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

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

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.breathe();
        d.bark();

        Cat c = new Cat();
        c.breathe();
        c.meow();
    }
}
Breathing...
Barking...
Breathing...
Meowing...

Explanation: Both Dog and Cat classes inherit from Animal. This forms a hierarchical structure where multiple subclasses share a common superclass.

Method Overriding in Inheritance

Sometimes the child class needs to provide its own version of a method inherited from the parent class. This is known as method overriding.

class Animal {
    void sound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();  // calls overridden method
    }
}
Dog barks

Explanation: Even though a is an Animal type reference, it points to a Dog object. Hence, the overridden method in Dog is executed.

Important Points to Remember

Conclusion

Inheritance lets us reuse code and model real-world relationships.

QUIZ

Question 1:Which keyword is used in Java to create an inheritance relationship between two classes?

Question 2:Java supports multiple inheritance through classes.

Question 3:What are the benefits of using inheritance in Java?

Question 4:What will be the output of the following code?
class Animal {
    void sound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}

Question 5:Constructors are inherited in Java subclasses by default.

Question 6:Which of the following are valid types of inheritance supported by Java classes?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M