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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay