简体   繁体   English

当子类和超类都实现相同的接口时发生了什么

[英]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

  • The interface defines " What methods should be there" 界面定义“应该有哪些方法”
  • The parent class defines the method 父类定义方法
  • The child class defines and overrides the method defined in the parent class 子类定义并覆盖父类中定义的方法

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.

相关问题 当我从抽象超类活动的子类中调用方法时发生了什么? - What happened when I called a method from a subclass from the abstract superclass activity? 当超类没有实现接口而是子类时,在接口子类和超类之间进行转换 - Casting between interface sublass and superclass when superclass is not implementing interface but subclass is 什么时候子类化AbstractBorder以及什么时候实现Border接口? - When to subclass AbstractBorder and when to implement the Border interface? 实现接口时获取与超类相同的接口 - when implementing interfaces get same interface as superclass 同一包和不同包中超类的子类之间的区别是什么? - What is distinction between subclass of superclass in same package and different package? 即使超类实现了相同的接口,在子类中实现接口是否有任何好处 - Is there any benefit in implementing a interface in a subclass even though the superclass implements the same interface 将子类对象传递给具有超类参数的方法时会发生什么? - What happens when a subclass object is passed to a method with a superclass parameter? 将子类对象分配为超类对象时会发生什么 - What happens when a subclass object is assigned as a superclass object 当子类更改超类方法的实现时,它叫什么? - What is it called when a subclass changes the implementation of a superclass method? 子类和超类中具有相同名称的变量 - Variable with same name in subclass and superclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM