DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Inheritance & Method Overriding - [Java OOP #4]
rachel
rachel

Posted on

Inheritance & Method Overriding - [Java OOP #4]

Inheritance

The word "inheritance", is defined as deriving something from one's ancestors. Similarly, in Java, this concept of inheritance can be applied as when a child class(known as a subclass) inherits attributes and methods from a parent class(known as a superclass).

Inheritance is useful when we want to create objects that have similar characteristics, but each object to have their own additional features. For example, we can have a Bicycle superclass, but we want multiple subclasses such as MountainBicycle and RoadBicycle to define specific types of bicycles.

In the example below, we will create a Cat subclass from the Animal superclass by using the extends keyword.

class Animal {
    public String name;
    public int age;

    Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println("Nom nom.");
    }

    public String toString() {
        return ("Name: " + name + "Age: " + age);
    }
}

class Cat extends Animal {
    public String colour;

    Cat(String name, int age, String colour) {
        // invoking superclass' constructor
        super(name, age);
        this.colour = colour;
    }

    // Overriding toString() method to print more info
    @Override
    public String toString() {
        return ("Name: " + name + "Age: " + age + "Colour: " + colour);
    }
}

public class Test {
    public static void main(String args[]) {
        Cat cat1 = new Cat("Oatmew", 3, "orange");
        System.out.println(cat1.toString());
        // calling a method from superclass
        cat1.eat();
    }
}

Enter fullscreen mode Exit fullscreen mode

Things to note:

  • name and age in Animal class were set to public because if it was private, its subclasses cannot access them.

  • Object cat1 has access to the eat() method. This is because the Cat class inherits the methods of the Animal class.

  • The super keyword is used in subclasses to access superclass members. It can be used to access attributes, constructors and methods.

  • @Override is an annotation used to inform the compiler that the subclass is overwriting the superclass. It is useful because it extracts a warning from the compiler if the method did not actually override anything and it also improves code readbility. It is not mandatory but generally a good practice to use it.


Types of inheritance

On the basis of class, there are 3 types of inheritance in Java:

  • Single

In a single level inheritance, a superclass only has one subclass.

Single level inheritance

  • Multilevel

In a multilevel level inheritance, a superclass can have a subclass and the subclass can have its own subclass, so on and so forth. However, each subclass only has access to its immediate parent class.

Multilevel level inheritance

  • Hierarchical

In a hierarchical inheritance, a single class serves as the superclass to multiple subclasses.

Hierarchical inheritance


Method Overriding

Method overriding is done when a subclass has a same method but with different implementation as the parent class. The rules for using method overriding are:

  • Only inherited methods can be overriden.
  • The overriding method must have the same name, paramters and return type as the overriden method.
  • A final, static or private method cannot be overriden.

Top comments (0)

Hey 😍

Want to help the DEV Community feel more like a community?

Head over to the Welcome Thread and greet some new community members!

It only takes a minute of your time, and goes a long way!