简体   繁体   中英

Which Method will get override from interface or class?

In the below program I had extended Demo1 class and implemented Demo interface and in Practice class I override public void demo() which was declared in both class and interface then from which that method will get Override? and why?

 interface Demo{
        void demo();
    }

class Demo1{
    int i=10;
    public void demo() {
       System.out.println("this is demo"+i);
    }
}
public class practice extends Demo1 implements Demo {
    public static void Main(String[] args ) {
        practice p=new practice();
        p.demo();
               
    }
    
    public void demo() {
        System.out.println("This is Overrided method");
    }
}

The short answer is that it doesn't matter. What the compiler is is looking for is that the signature of the abstract interface method is implemented in your class, or inherited from a supertype (and it doesn't care whether that inherited signature was meant to implement the abstract method in question).
And whether your demo() method is called on a practice object declared as Demo or Demo1 is also irrelevant the method signature is implemented either way.

You can, in fact, even remove your demo() override (assuming you didn't need to change the behavior), and the code would still compile:

class practice extends Demo1 implements Demo {
    public static void Main(String[] args) {
        practice p = new practice();
        p.demo();
    }
}

That is, even if Demo1.demo() has nothing to do with Demo.demo() , the fact that practice inherits Demo1.demo() which has the same signature as Demo.demo() and without violating access and exception constraints, that makes practice a valid implementation of Demo .

Method from interface is not overrided , it's implemented .

Your practice class inherits the demo method from Demo1 class and this method will be used to implement Demo interface method if you omit the implementation of demo method in practice class itself.

Since you implement the demo method in practice class itself - this method overrides the method from Demo1 class and also implements the method from Demo interface

If you run the below code

public class practice extends Demo1 implements Demo {
public static void main(String[] args ) {
    practice p=new practice();
    p.demo();
           
}

public void demo() {
    System.out.println("This is Overrided method");
}
}

Output will be "This is Override method" and if you comment the demo method like below

public class practice extends Demo1 implements Demo {
public static void main(String[] args ) {
    practice p=new practice();
    p.demo();
           
}

/*public void demo() {
    System.out.println("This is Overrided method");
}*/
}

Output will be "this is demo10". In first case as class object finds definition of demo in self, it will give preference to this over to parent class.

Demo1 is providing implemetation of demo(). so it will take parent demo() method. Output will be

this is demo10

But then you have implemented again overriding of demo() method in child class practice

so output is now

This is Overrided method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM