Question

Class A has run() method and interface B also has run() method. Question is simple, which run() method is overridden in Main class and how will we prove this? Why there is no conflict (Compile-time error) in this code?

class A{
    void run(){System.out.println("A class");}
}

interface B{
    void run();
}

class Main extends A implements B{
    public static void main(String args[]){         
        Main m = new Main();
        m.run();
    }

    // Overridding method
    public void run(){
        System.out.println("run method");
    }
}
Was it helpful?

Solution

run of A is overridden, run of B is implemented. As B is an interface, it only states how your objects behave to others and don't enforce any behavior itself.

OTHER TIPS

run() Method of Interface B will be Implemented in class Main by overridden method of Class A.

Adding an extra point,

It you will not write the run() method in child class Main, You will not get well known "Unimplemented methods" error. This is true for public methods of class A for non public methods you will get compiler error : The inherited method can not hide public abstract method.

That is because methods of interface are public by default and you can not hide it with default (package private) access modifier.

Sample :

class A{
    public void run() {
        System.out.println("This method will be inherited.");
    }
}

interface B{
    void run();
}

class Main extends A implements B{
    public static void main(String args[]){         
        Main m = new Main();
        m.run();
    }
}

OUTPUT : This method will be inherited.

In above code instance, the run() method is inherited from class A that will implement run() method of interface B.

What will be called is

public void run(){
    System.out.println("run method");
}

Why?

You're implementing run of interface B and you're also overriding the A implementation of it.

If you remove the last run() implementation and remove implements B, run() of A will be called.

Interface B says that any class implementing it must have a run method. Main extends A and inherits A's run() method.

Main meets the requirement of the B interface for having a run method. The run method in Main then overrides the run() method it got from the A class.

// This is a class with a run method.
class A{
    void run(){System.out.println("A class");}
}

// Anything implementing this interface must have a run method.
interface B{
    void run();
}

// This class is an extension of A (and therefore has A's run() method)
// This class implements B and must have a run method, which it does, because it has A's
class Main extends A implements B{
    public static void main(String args[]){         
        Main m = new Main();
        m.run();
    }

    // This method then overrides the run Method it inherited from A
    // And this method meets the requirement set by implementing B.
    public void run(){
        System.out.println("run method");
    }
}

There is no conflict because both methods have the same signature. Method declarations in interfaces are not overridden because they don't implement anything. So in this case, run method in class A is overridden.

On the other hand, when overriding methods you are encouraged to use the @Override annotation, e.g.

@Override
public void run() {
   ....
}

Super class's method is always called as overridden method whereas subclass method is called as overriding method.

Final method cannot be overridden. [if superclass's method is final]
Final methods can override. [Read it as grammatical way. This method is in subclass and is final, but superclass's method is not final]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top