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

前端 未结 5 655
情话喂你
情话喂你 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:05

    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.

    0 讨论(0)
  • 2021-01-24 20:06

    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.

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2021-01-24 20:21

    Interface is not inherited - it is rather implemented

    0 讨论(0)
  • 2021-01-24 20:23

    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.
    0 讨论(0)
提交回复
热议问题