DEV Community

Isaac Tonyloi
Isaac Tonyloi

Posted on

Method Overriding in Java.

Connect with me Isaac Tonyloi.
A Software Engineer<br>
Photo by Nubelson Fernandes on Unsplash

Method overriding can be achieved through inheritance in java. Inheritance allows us to derive a new class from a preexisting class. Inheritance also promotes code reusability by allowing one class known as the sub class or child class to inherit methods and fields from another class known as the super class or the parent class.

Now method overriding occurs when a method is defined with the same name both in the subclass and the super class. Here is an example of method overriding in java.

class Bicycle {
    public void brake() {
        System.out.println("All bikes should have breaks");
    }
}

class MountainBicycle extends Bicycle {
    public void brake() {
        System.out.println("Mountain bicycle should also have brakes");
    }
}

class Main {
    public static void main(String[] args) {

        MountainBicycle obj = new MountainBicycle();

        obj.brake();
    }
}
Enter fullscreen mode Exit fullscreen mode
Mountain bicycle should also have brakes
Enter fullscreen mode Exit fullscreen mode

In the above example the method brake() is both in the super class and the subclass. Therefore the method in the subclass overrides the method in the super class. In such scenarios we should use the @Override annotation, however it is not mandatory.
However when using the @Override annotation we should be keen to ensure that:

We are not overriding methods that have been declared static or final.
Neither should we Override methods with different return types, parameters or name for that sake.
We can access the same method defined in the superclass by using the keyword super() as shown below. The same keyword can also be used to call a constructor in the super class from the subclass.

class Mfs {
    public void display() {
        System.out.println("User payment systems");
    }
}

class Kplc extends Mfs {
    @Override
    public static display() {
        System.out.println("Kenya power tokens payment system");
    }

    super.display();
}

class Main() {
    public static void main(String[] args) {
        Kplc obj = new Kplc();

        obj.display();
    }  
}

Enter fullscreen mode Exit fullscreen mode
All bikes should have breaks
Mountain bicycle should also have brakes

Enter fullscreen mode Exit fullscreen mode

We should also note that it is in order to override methods with different access specifiers. However we can only do so if the method in the subclass has a more broader access specifier than the one used in the superclass method. For instance in the example below the method in the super class has ‘protected’ as the class specifier while that in the subclass has public which provides larger access. This allows us to override the method in the super class as shown below.

class Bicycle {
    protected void brake() {
        System.out.println("All bikes should have breaks");
    }

}

class MountainBicycle extends Bicycle {
    public void brake() {
        super.brake();
        System.out.println("Mountain bicycles should also have brakes");


    }
}

class Main {
    public static void main(String[] args) {

        MountainBicycle obj = new MountainBicycle();

        obj.brake();
    }
}

Enter fullscreen mode Exit fullscreen mode
All bikes should have breaks
Mountain bicycles should also have brakes

Enter fullscreen mode Exit fullscreen mode

Not every method can be overridden however abstract methods should always be overridden

Top comments (0)