英文:
Which Method will get override from interface or class?
问题
在下面的程序中,我扩展了Demo1类并实现了Demo接口,在Practice类中,我重写了public void demo()
方法,该方法在类和接口中都有声明,那么这个方法将会被哪个重写?为什么?
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 Overridden method");
}
}
英文:
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");
}
}
答案1
得分: 1
这个问题的简短回答是:不重要。编译器要查找的是,抽象接口方法的签名在你的类中得到了实现,或者从父类型继承而来(而且它不关心这个继承的签名是否本来就是为了实现所涉及的抽象方法)。而且无论你的 demo()
方法是在声明为 Demo
还是 Demo1
的 practice
对象上调用,方法的签名都会被实现,这两种方式都是无关紧要的。
事实上,你甚至可以移除 demo()
的重写(假设你不需要改变行为),代码仍然会编译通过:
class practice extends Demo1 implements Demo {
public static void Main(String[] args) {
practice p = new practice();
p.demo();
}
}
也就是说,即使 Demo1.demo()
与 Demo.demo()
没有任何关联,但是 practice
继承了 Demo1.demo()
,它的签名与 Demo.demo()
相同,并且没有违反访问和异常约束,这使得 practice
成为了 Demo
的一个有效实现。
英文:
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). <br/>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
.
答案2
得分: 0
接口中的方法没有被重写,而是被实现。
你的practice类继承了Demo1类中的demo方法,如果你在practice类本身中省略了demo方法的实现,那么这个方法将会被用来实现Demo接口中的方法。
因为你在practice类中实现了demo方法 - 这个方法重写了Demo1类中的方法,并且还实现了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
答案3
得分: 0
以下是翻译好的部分:
如果您运行以下代码
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");
}
}
输出将是"This is Override method",如果您像下面这样注释掉demo方法
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");
}*/
}
输出将是"this is demo10"。在第一种情况下,由于类对象在自身中找到了demo的定义,它会优先选择自身的定义,而不是父类的定义。
英文:
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.
答案4
得分: 0
Demo1提供了demo()的实现。因此,它将采用父demo()方法。输出将为
>这是demo10
但是后来你在子类practice中再次实现了对demo()方法的覆盖,
因此现在输出为
>这是被覆盖的方法
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论