Logo

dev-resources.site

for different kinds of informations.

Method Overriding in Java.

Published at
1/12/2022
Categories
java
inheritance
methodoverriding
methods
Author
isaacttonyloi
Author
13 person written this
isaacttonyloi
open
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

inheritance Article's
30 articles in total
Favicon
Code Smell 286 - Overlapping Methods
Favicon
Understanding Traits in PHP and How They Differ from Inheritance
Favicon
Understanding Classes and Inheritance in JavaScript
Favicon
Mastering Generalization in OOP: Techniques and Examples
Favicon
Upcasting — Using a Superclass Reference for a Subclass Object
Favicon
Mapping inheritance hierarchies with MapStruct
Favicon
Python Inheritance Explained: Types, Examples, and Best Practices
Favicon
Mastering TypeScript: Understanding the Power of extends
Favicon
PHP: Herança vs. Composição
Favicon
Method Resolution Order in Python 3
Favicon
Why we don't use RemoteWebDriver driver = new ChromeDriver()
Favicon
Understanding JavaScript Inheritance: A Deep Dive into Prototypal and Constructor Patterns
Favicon
Understanding Inheritance and Polymorphism: Simplified OOP Concepts
Favicon
JS Inheritance - Part 2: Factory Functions vs. Classes
Favicon
Inheritance with access-specifier in cpp
Favicon
Inheritance in Dart
Favicon
RoR - extend, < (inheritance), include - know the difference
Favicon
Inheritance vs composition: a fight against Egyptian gods
Favicon
Object Oriented Concept - Inheritance
Favicon
Exploring Component Inheritance in Aventus
Favicon
Python Inheritance
Favicon
Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example
Favicon
PostgreSQL - Partitioning & Inheritance
Favicon
Prototype and Prototypical Inheritance
Favicon
Ruby - Inheritance
Favicon
MultiLevel Inheritance in Java
Favicon
Overloading vs Overriding in Typescript
Favicon
Java Error - at least one public class is required in main file
Favicon
Polymorphism in Java.
Favicon
Method Overriding in Java.

Featured ones: