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");
}
}
当两个类都在实现抽象方法getVehicle()
时,这里实际发生了什么?子类是否重写超类getvehicle()
或Inteface getVehicle()
?
答案 0 :(得分:5)
子类的实现将覆盖超类的实现。没有必要说子类会覆盖接口的方法,因为接口不提供实现(除非你在谈论Java 8的默认接口方法)。
顺便说一下,它足以声明超类实现了接口。子类将隐式实现它,而不声明它实现它。所以即使你写:
public class HelloWorld implements Vehicle
和
class MyWorld extends HelloWorld
MyWorld
仍然实施Vehicle
。
答案 1 :(得分:1)
所以,它的行为就像任何其他继承一样
(即使接口[Java 8]中有默认方法实现)
尽管在子类中实现接口(当父类已实现相同的接口时)对行为没有影响。这样做有助于提高某些情况下的可读性。