Method Overriding in Java.
Tags: java, inheritance, methodoverriding, methods
Published: 2022-01-12 12:02:47


Connect with me [Isaac Tonyloi](https://www.linkedin.com/in/isaactonyloi/).
![A Software Engineer
  ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2osolals5wiyyojgzdf.jpg)
Photo by <a href="https://unsplash.com/@nublson?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Nubelson Fernandes</a> on <a href="https://unsplash.com/s/photos/code?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
  
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();
    }
}
```

```
Mountain bicycle should also have brakes
```

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

```

```
All bikes should have breaks
Mountain bicycle should also have brakes

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

```

```
All bikes should have breaks
Mountain bicycles should also have brakes

```

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