Rekha
Rekha

Reputation: 1433

Calling no argument constructor of super class

If a class does not provide any constructor,the compiler will provide a default constructor,which in turn call's the no-argument of the super class. Why is this call to the no-argument constructor of the super class needed?

Upvotes: 0

Views: 1032

Answers (4)

Jason S
Jason S

Reputation: 189906

It's part of the "is-a" relationship of subclass instances to their superclass. If a SpottedFoo is a subclass of Foo (e.g. each SpottedFoo "is-a" Foo), then the Foo constructor should be called.

Otherwise, if there are initializations (private or not) performed by the Foo constructor that are skipped, there's no way a SpottedFoo can guarantee it can act like a Foo.

That's part of the contract for implementation inheritance: each subclass instance must be able to act like any other members of their superclass. This is the Liskov substitution principle.

Upvotes: 0

mre
mre

Reputation: 44250

Does it make sense for an object's constructor to only partially construct the object?

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80192

So that you can initialize the super class properties as well.

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240996

To initialize the super (inherited) part of that class

Upvotes: 4

Related Questions