Reputation: 1433
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
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
Reputation: 44250
Does it make sense for an object's constructor to only partially construct the object?
Upvotes: 1
Reputation: 80192
So that you can initialize the super class properties as well.
Upvotes: 2