Inheritance in Java
Syntax and Examples

Inheritance in Java

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.

Types of Inheritance in Java

Java supports the following types of inheritance:

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

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

Syntax of Inheritance

This syntax shows how a class inherits from another using the extends keyword. The Child class gains access to all the fields and methods of the Parent class, and can also define its own.

class Parent {
    // fields and methods
}

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

Single Inheritance

This example shows how a subclass inherits methods from a single superclass. The Dog class inherits the sound() method from Animal, and also defines its own method bark(). In the main method, both inherited and own methods are accessible through the Dog object.

  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

Multilevel Inheritance

This example demonstrates multilevel inheritance, where a class inherits from a derived class, forming a chain. The class Puppy inherits from Dog, which in turn inherits from Animal. As a result, Puppy has access to methods of both Dog and Animal.

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...

Hierarchical Inheritance

This example demonstrates hierarchical inheritance where multiple classes (Dog and Cat) inherit from a single base class (Animal). Both subclasses reuse the breathe() method from Animal, while also having their own specific methods (bark() and meow()).

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...

Method Overriding in Inheritance

When a subclass provides its own implementation of a method that is already defined in its parent class, it is called method overriding. This allows the subclass to define behavior specific to its type.

The @Override annotation is used to indicate that a method is being overridden.

In the example, the Dog class overrides the sound() method of the Animal class. When sound() is called on an Animal reference pointing to a Dog object, the overridden method in Dog executes, demonstrating runtime polymorphism.

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

Why Use Inheritance?

  • To avoid code duplication
  • To establish a natural hierarchy between classes
  • To allow for method overriding and polymorphism
  • To enhance maintainability and scalability

Important Points to Remember

  • Java only supports single inheritance through classes.
  • Use super to call parent class methods or constructors.
  • Overridden methods in subclasses are called based on the actual object type at runtime (polymorphism).
  • Constructors are not inherited but the parent constructor can be called using super().

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?