Charan
Charan

Reputation: 15

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

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?

Upvotes: 1

Views: 12058

Answers (5)

user7343218
user7343218

Reputation: 1

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.

Upvotes: 0

exexzian
exexzian

Reputation: 7890

Interface is not inherited - it is rather implemented

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533530

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.

Upvotes: 5

Fritz
Fritz

Reputation: 10055

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

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382170

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.

Upvotes: 2

Related Questions