Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Isaac Tonyloi
Isaac Tonyloi

Posted on

     

Method Overriding in Java.

Connect with meIsaac Tonyloi.
A Software Engineer<br>
Photo byNubelson Fernandes onUnsplash

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 modeExit fullscreen mode
Mountain bicycle should also have brakes
Enter fullscreen modeExit 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 modeExit fullscreen mode
All bikes should have breaksMountain bicycle should also have brakes
Enter fullscreen modeExit 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 modeExit fullscreen mode
All bikes should have breaksMountain bicycles should also have brakes
Enter fullscreen modeExit fullscreen mode

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer
  • Location
    Kenya
  • Education
    Kenyatta university
  • Work
    Software Developer and Data Scientist.
  • Joined

More fromIsaac Tonyloi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp