Interface does not have constructor, then how can it be inherited?

扶醉桌前 提交于 2019-12-20 04:29:07

问题


As I know the subclass constructor calls the super class constructor by using super();. But since interface doesn't have any constructor, how can inheritance take place?


回答1:


But since interface doesn't have any constructor how can inheritance take place??

Easy, an interface cannot have any instance fields so there is nothing to construct. You cannot place in code in an interface (up to Java 7 anyway) so there is nothing which needs to be called.




回答2:


The interface is a contract, defining what methods mush be offered by the implementation. A class doesn't inherit an interface but implements it.

From the specification :

This type has no implementation, but otherwise unrelated classes can implement it by providing implementations for its abstract methods.




回答3:


Interfaces (also known as Service Contracts) are implemented, not constructed. They define a set of methods (Services) that a class provides, so a client knows what can he expect of the implementing class regardless of the actual type implementing the interface. The constructor is related to this particular instance of a given type, implementing the interface.

IYourObject yourObject = new YourObject();

On the other hand, interface inheritance is also by extension. It "adds" the methods of an interface to another one and allow the possibility of switching the interface type of an object amongst the different interfaces in the "hierarchy".




回答4:


Interface is not inherited - it is rather implemented




回答5:


Interface doesn't contain constructor because of the following reasons:

  • The data member of the interface are already initialized .
  • Constructors of the special defined methods which are not permitted to defined and moreover interface data member are static.


来源:https://stackoverflow.com/questions/14709696/interface-does-not-have-constructor-then-how-can-it-be-inherited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!