Can a same method exist in a class twice, through Inheritance and interfaces?

大兔子大兔子 提交于 2020-02-24 17:35:47

问题


Here is the scenario:-

class Canine{
  public void roam(){
    System.out.println("Canine-Roam");
  }
}

public interface Pet{    
  public abstract void roam();
}


class Dog extends Canine implements Pet{

  public void roam(){
    System.out.println("Dog Roam");
  }
  public static void main(String [] args){
    Dog adog = new Dog();
    adog.roam();
  }
}

I am aware that JVM must not have any confusion in choosing which method to run, that means, which method gets over-ridden. But I am confused anyway. Why does this program compile?


回答1:


No - the same method cannot exist in a class twice.

An interface simply declares a requirement for a class to implement a particular method. It does not actually create that method.

So a class that acquires a method implemention through inheritance has that method defined. This (single) implementation satisfies the interface's requirements.

In your case:

  1. Dog extends Canine, so it means that the roam() method from Canine is available, and would be exposed as a method on Dog objects if not overridden.
  2. But then Dog then overrides the superclass' method with its own definition of roam(). This is allowed, and there is still just one unambiguous method called roam() on Dog - the new override.
  3. Dog implements Pet, which means it is required to have a roam() method. It does - so it's a valid implementation of this interface.



回答2:


Your case is totally fine and it will run fine and outputs Dog Roam. That means the function in Dog class runs.

You did not get any compile time errors because the method in Dog is implementing the abstract method declared in interface and coincidentally the method signature of this method matches with the parent class.




回答3:


I think you confuse two things:

  • Implementation: what is done? see: Canine.roam() and Dog.roam()
  • Interface: how do you invoke it? see: Pet.roam()

It's clear that you have two "implementations" of roam() in two classes:

  • Canine.roam()
  • Dog.roam()

There are never two same methods in same class.

And because Dog extends Canine, the method Canine.roam() got overriden. Your main() function uses Dog.roam() instead.




回答4:


You had created object for Dog type and assigned it to Dog reference type.
Overridding, has nothing to do here. Though you extended Dog class from canine, both are 2 different types.
At compile time, the compiler checks whether the method definition is present in Reference type. Thats it. And it has to be present there. At run time, JVM checks for the method in reference type first, then it finds is there any over ridden version for the same method present in "Reference type's " subclass.
If present, it will be executed. Or else, Reference type's method will be executed.

i.e.

Canine c=new Dog(); 

will execute Dog's method.



来源:https://stackoverflow.com/questions/13084980/can-a-same-method-exist-in-a-class-twice-through-inheritance-and-interfaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!