Java中接口实现的继承 [英] Inheritance of Interface implementation in Java

查看:146
本文介绍了Java中接口实现的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中的接口有两个问题。
1)如果一个类碰巧实现了接口I的所有接口方法,而没有声明自己实现它们,它仍然可以用作I类变量的输入吗?
2)实现接口I的A类的子类是否继承了该接口的一致性,还是应该声明自己实现I?

I have two questions regarding interfaces in Java. 1) If a class happens to implement all the interface methods of interface I, without declaring itself as implementing them, can it still be used as input into variables of type I? 2) Does a subclass of class A which implements interface I inherits the conformance to that interface, or should it also declare itself as implementing I?

推荐答案


如果一个类碰巧实现了接口I的所有
接口方法,
未声明为
实现它们,是否仍然可以将
用作
类型变量的输入?

If a class happens to implement all the interface methods of interface I, without declaring itself as implementing them, can it still be used as input into variables of type I?

没有。你所描述的更类似于鸭子打字

No. What you're describing is more akin to duck typing.



实现接口的类A的子类是否继承了该接口的
一致性,或者如果它也声明为

实施我?

Does a subclass of class A which implements interface I inherits the conformance to that interface, or should it also declare itself as implementing I?

假设你的意思是:

public class A implements I { /* ... */ }

public class B extends A { /* ... */ }

在这种情况下, B 实现 I

这篇关于Java中接口实现的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆