[英]What happened when subclass and superclass both implement a same interface
interface Vehicle
{
public abstract void getVehicle();
}
public class HelloWorld implements Vehicle{
@Override
public void getVehicle()
{
System.out.println("HelloWorld Implementation");
}
}
class MyWorld extends HelloWorld implements Vehicle
{
@Override
public void getVehicle()
{
System.out.println("MyWorld Implementation");
}
}
When Both Classes are Implementing the abstract method getVehicle()
, what is actually happening here ? 当两个类都在实现抽象方法
getVehicle()
,这里实际发生了什么? Is the sub-class overriding super-class getvehicle()
, or Inteface getVehicle()
? 子类是否重写了超类
getvehicle()
或getVehicle()
?
The sub-class's implementation would override the super-class's implementation. 子类的实现将覆盖超类的实现。 There's no meaning to saying the sub-class would override the interface's method, since the interface doesn't supply an implementation (unless you are talking about Java 8 default interface methods).
说子类会覆盖接口的方法是没有意义的,因为接口不提供实现(除非你在谈论Java 8默认接口方法)。
BTW, it's enough to declare that the super-class implements the interface. 顺便说一句,它足以声明超类实现接口。 The sub-class would implement it implicitly without declaring that it implements it.
子类将隐式实现它,而不声明它实现它。
So even if you write : 即使你写道:
public class HelloWorld implements Vehicle
and 和
class MyWorld extends HelloWorld
MyWorld
still implements Vehicle
. MyWorld
仍然实现了Vehicle
。
So, it would behave just like any other inheritance 所以,它的行为就像任何其他继承一样
(even if the default method implementation is available in interface [Java 8]) (即使接口[Java 8]中提供了默认方法实现)
Although, implementing the interface in the child class (when the parent already implements the same interface) has no impact on behavior. 虽然,在子类中实现接口(当父类已实现相同的接口时)对行为没有影响。 Doing so helps improve readability in some cases.
这样做有助于提高某些情况下的可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.