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

前端 未结 5 653
情话喂你
情话喂你 2021-01-24 19:43

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

5条回答
  •  执笔经年
    2021-01-24 20:08

    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".

提交回复
热议问题