Anwser for I: if you have an existed class, however, you want to make it to belong to another new type. You can create a new subclass which extends the existed class(reuse) and implements the new interface type. Therefore, this new interface help you create a new TYPE. You can understand this better when you touch some patterns of OOP.
if you have some idea about design pattern, such as Factory & Abstract Factory, it is good for you to understand interface.interface is very useful, when you need special method for the object, and the type of object is not sure.
if you have class A B implements interface C.There is no relationship between A and B.the following codes is useful.C variableName1 = new A(); C variableName2 = new B();or sometimes you can cast the Object to speical interface typeInterfaceName variableName1 = InterfaceName(Vector.get());
By the way, Java is based on Single inheritance (SmallTalk) rather than multiple inheritance (C++).
C variableName2 = new B();or sometimes you can cast the Object to speical interface typeInterfaceName variableName1 = InterfaceName(Vector.get());
一些问题。二 保持继承树的清晰明朗,给后续子类提供一个统一的,抽象层次较高的公用类型。
基于此可以实现更为清晰的多态。三 可以用来实现类似C++,delphi等语言中callback的功能。如果你对AWT实践模型
比较清楚,那么你应该明白interface在java中有多重要。其实在这时候,interface
实际上就充当了C++/delphi里面的函数/过程 指针。四 在软件工程方面,设计与实现分离,更容易编写和维护代码。你可以简单看一些例子,比如:定时器的实现,事件监听接口,钩子等
COM里面的一些例子也可以说明interface的好处。
关于interface和C++的多继承,我认为没有任何关系。正如cc9hwa(rubbish)说的,这两个不同门不同路的东西,有什么好比较的。
dcren(稻草人)的三四点比较有理。