DEV Community

Marcos Gabriel de Oliveira Favaretto
Marcos Gabriel de Oliveira Favaretto

Posted on

2

Java's priority for inheritance of methods

Introduction

That post was based on Vogella's post about an introduction of Java.

Multiple Inheritance of Methods.

Java follows an especific priority order to decide which method should be used among the classes (Super Classes or Interfaces) that have their implementations.

  1. Overrides methods.
  2. Super Class methods.
  3. Subtype methods (instead of Supertypes one).

For example, let's consider the following Interfaces and Super Class:

interface MyFirstInterface { // Supertype
    public default void myMethod(String myParameter) {
        System.out.println("First Message: " + myParameter);
    }
}

interface MySecondInterface extends MyFirstInterface { // Subtype
    public default void myMethod(String myParameter) {
        System.out.println("Second Message: " + myParameter);
    }
}

class MySuperClass {
    public void myMethod(String myParameter) { // Super class
        System.out.println("Super Class Message: " + myParameter);
    }
}
Enter fullscreen mode Exit fullscreen mode

Using MySuperClass as Super Class, implemmenting both Interfaces and overriding the myMethod method in a test class, according to the priority order, it is expected that the overrided method will be executed.

Code:

class Application extends MySuperClass implements MyFirstInterface, MySecondInterface {
    public static void main(String args[]) {
        Application myApp = new Application();
        myApp.myMethod("Hello World!");
    }

    @Override
    public void myMethod(String myParameter) {
        System.out.println("Overrided Message: " + myParameter);
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Overrided Message: Hello World!
Enter fullscreen mode Exit fullscreen mode

Using MySuperClass as Super Class and implemmenting both Interfaces at the same test class, according to the priority order, it is expected that the Super Class's method will be executed.

Code:

class Application extends MySuperClass implements MyFirstInterface, MySecondInterface {
    public static void main(String args[]) {
        Application myApp = new Application();
        myApp.myMethod("Hello World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Super Class Message: Hello World!
Enter fullscreen mode Exit fullscreen mode

Implemmenting only both Interfaces at the test class, according to the priority order, it is expected that the Subtype's method (MySecondInterface.myMethod) will be executed.

Code:

class Application implements MyFirstInterface, MySecondInterface {
    public static void main(String args[]) {
        Application myApp = new Application();
        myApp.myMethod("Hello World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Second Message: Hello World!
Enter fullscreen mode Exit fullscreen mode

As shown above, this is the list of priorities that Java assumes when encountering these scenarios. You can see the code by clicking here.

Any suggestions are accepted, thank you for reading ;D.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay