Java接口和继承 [英] Java interface and inheritance
问题描述
如果我们有:
public interface Foo{}
public class Bar implements Foo{...}
之间是否存在差异:
public class BarBar extends Bar implements Foo{..}
和
public class BarBar extends Bar{..}
我看到很多像这样的代码,它总是让我感到困惑。 BarBar
是否需要实现 Foo
?我的意思是,因为它已经扩展 Bar
开始不是那里已经存在?我想我的问题是,在 BarBar
中实施 Foo
的目的是什么?
I see a lot of code like this and it always confuses me. Does BarBar
need to implement Foo
? I mean since it extends Bar
to begin with isn't that already there? I guess my question is, what purpose does implementing Foo
in BarBar
serve here?
推荐答案
主要区别在于15个完全不必要的字符: - )
The main difference is 15 completely unnecessary characters :-)
当你的父类实现了一些接口时,所有接口方法都可以由它实现,也可以(显式或隐式)定义为抽象。无论哪种方式,扩展父类的类都会继承所有这些方法并隐式实现原始接口。
When your parent class implements some interface, all interface methods are either implemented by it or are defined (explicitly or implicitly) as abstract. Either way, your class extending the parent class inherits all those methods and implicitly implements the original interface.
这篇关于Java接口和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!