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.
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
Joined: Jan 19, 2013
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?
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.