Inheritance & Method Overriding - [Java OOP #4]
Tags: computerscience, oop, beginners, java
Published: 2022-10-06 02:45:10

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

```java
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();
    }
}

```
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38t3mtkbymddyol503ao.png)

+ **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh5yti57bko91w2f60cm.png)

+ **Hierarchical**

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

![Hierarchical inheritance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tksilabbp8z0kfbhxls2.png)

---

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