please consider the following question from certpal mock simulator test 2 question no . 1/68
why it gives compiler error ? i know that there is default no arg constructor of cert and there is no NO-ARG constructor in Marmaduke, but what i thought was that since we are not calling Cert constructor so there will not be any error. i.e we are just creating object of Marmaduke, there is no call to cert constructor. so it shouldn't give any error. further certpal says "When extending a class always ensure that it has a public constructor, since the compiler will try to call it by default ". but i didn't understood. please help , why it happens like this ?
You don't have to add a no-arg constructor in Marmaduke. But if you don't, you need to make sure that any constructor in Cert calls an existing constructor.
As you say, if you don't add your own constructor, a no-arg one is added for you. But also remember that a subclass constructor will always end up calling the superclass constructor. If you don't put an explicit call to super() in yourself, the compiler will insert a call to the no-arg superclass constructor. So, since you haven't provided a constructor in Cert, it's the equivalent of doing this:
As you can now see, it's trying to call a constructor that doesn't exist. The compiler doesn't bother trying to work out which superclass constructor to call, and what arguments it ought to pass in. Instead, it tries to call the no-arg one, and that causes the error.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop