java 接口和抽象类中的相同方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11296789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Same method in Interface and Abstract class
提问by Nandkumar Tekale
I came to situation :
我来到了情况:
public interface Intr {
public void m1();
}
public abstract class Abs {
public void m1() {
System.out.println("Abs.m1()");
}
// public abstract void m1();
}
public class A extends Abs implements Intr {
@Override
public void m1() {
// which method am I overriding, well it is Abs.m1() but why?
// if method implemented is Abs.m1(), then why I am not getting error for Intr.m1() not implemented.
}
}
回答by hvgotcodes
You are satisfying both conditions at once; ie. the one implementation is at the same time fulfilling the abstract class requirements and the interface requirements.
你同时满足这两个条件;IE。一种实现同时满足抽象类要求和接口要求。
As a note, unless you are using Intr
in another inheritance chain, you don't need it. Also, it might make sense to move the implements Intr
up to the abstract class definition.
请注意,除非您Intr
在另一个继承链中使用,否则您不需要它。此外,将implements Intr
向上移动到抽象类定义可能是有意义的。
回答by biziclop
You can only override methods defined in another class.
您只能覆盖在另一个类中定义的方法。
Methods declared in an interface are merely implemented. This distinction exists in Java to tackle the problem of multiple inheritance. A class can only extend one parent class, therefore any calls to super
will be resolved without ambiguity. Classes however can implement several interfaces, which can all declare the same method. It's best to think of interfaces as a list of "must have"s: to qualify as a Comparable
your cluss must have a compareTo()
method but it doesn't matter where it came from or what other interfaces require that same method.
在接口中声明的方法只是被实现。Java 中存在这种区别以解决多重继承的问题。一个类只能扩展一个父类,因此任何对 的调用都super
将被毫无歧义地解析。然而,类可以实现多个接口,它们都可以声明相同的方法。最好将接口视为“必须拥有”的列表:要成为Comparable
您的类必须拥有一个compareTo()
方法,但它来自何处或其他哪些接口需要相同的方法并不重要。
So technically you override Abs.m1()
and implement Intr.m1()
in one fell swoop.
因此,从技术上讲,您可以一举覆盖Abs.m1()
和实施Intr.m1()
。
Note that this would be fine too:
请注意,这也可以:
public class B extends Abs implements Intr {
//m1() is inherited from Abs, so there's no need to override it to satisfy the interface
}
回答by Vijay Gajera
Here both the interface and abstract class have same method.
这里接口和抽象类都有相同的方法。
You have one class name is hello and exteds abstract class and implement interface its true and you override meth1 method on hello class its fine and its compile correctly and not given any error but her you can't identify which class method is override like abstract class or interface.
你有一个类名是 hello 并且扩展抽象类并实现接口它的 true 并且你覆盖了 hello 类上的 meth1 方法它很好并且它的编译正确并且没有给出任何错误但是她你无法识别哪个类方法像抽象类一样被覆盖或界面。
This is runtime polymorphism you cant create object of abstract class and interface but you can create reference variable of that. Here solution is you can't identify that on compile time its actual override at run time.
这是运行时多态性,你不能创建抽象类和接口的对象,但你可以创建它的引用变量。这里的解决方案是您无法在编译时识别它在运行时的实际覆盖。
interface hi
{
public void meth1();
}
abstract class Hullo
{
public abstract void meth1();
}
public class Hello extends Hullo implements hi
{
public void meth1(){
System.out.println("hello");
}
hi h= new Hello();
h.meth1();//its means interface method is override. and its decide when we call method.
hullo hu= new Hello();
hu.meth1();//its means abstract class method is override.
}
回答by Alexey A.
@Override
ensures you override the method with no difference Interface or abstract superclass. So no error with override.
@Override
确保您覆盖没有区别接口或抽象超类的方法。所以覆盖没有错误。
On the other hand Interface method is also implemented in the superclass which is enough for Interface contracts.
另一方面,接口方法也在超类中实现,这对于接口合同来说已经足够了。