| Author |
constructor
|
binay shah
Greenhorn
Joined: Jan 19, 2013
Posts: 12
|
|
can any one tell why the code below does not compile?
The solution to this problem says "When extending a class always ensure that it has a public constructor, since the compiler will try to call it by default ".
when i put the default constructor in class Marmaduke it compiles.
but why does it need a default constructor when there isn't a subclass object instantiation with no arguments
thanks in advance.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
binay shah wrote:
subclass Cert has a default constructor, since you dont construct constructor explicitly . and the default constructor calls no argument super class's constructor as a first statement
as in below
|
 |
binay shah
Greenhorn
Joined: Jan 19, 2013
Posts: 12
|
|
thanks for your response.
i understand that there is a implied super() call in the Cert class but line 8 is calling the super class constructor directly. My understanding of the above code is that - "if line 8 was replaced by 'new Cert()', then there is a constructor chaining.
The problem i'm not understanding is that, where is the call to subclass constructor?
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
binay shah wrote:The problem i'm not understanding is that, where is the call to subclass constructor?
There is no call to the subclass constructor, but you have not included a constructor in the subclass and so the compiler automatically includes a no-arg constructor. The only thing this constructor does is call the no-arg constructor of its parent class. But the parent class does not have a no arg constructor, because you have included a 1 arg constructor in the parent class, so the compiler does not add a no arg constructor. It's the lack of a no arg constructor in the parent class that is called from the no arg constructor of the sub class that the compiler is complaining about.
|
Joanne
|
 |
binay shah
Greenhorn
Joined: Jan 19, 2013
Posts: 12
|
|
|
Thank you very much. I got it!
|
 |
 |
|
|
subject: constructor
|
|
|