• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in certpal mock ?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write an arg-constructor then compiler doesn't provide default constructor.You have to write that also.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:When you write an arg-constructor then compiler doesn't provide default constructor.You have to write that also.


is it compulsary to write default constructor in Marmaduke ?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
saloni jhanwar wrote:When you write an arg-constructor then compiler doesn't provide default constructor.You have to write that also.


is it compulsary to write default constructor in Marmaduke ?


Object creation of Marmaduke will ultimately make a call to default constructor of Marmaduke.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic