File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem in certpal mock ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem in certpal mock ?" Watch "problem in certpal mock ?" New topic
Author

problem in certpal mock ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Feb 09, 2012
Posts: 583

When you write an arg-constructor then compiler doesn't provide default constructor.You have to write that also.


Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in certpal mock ?
 
Similar Threads
constructor up and down hierarchy
Co variant returns
Overriding Fragments
Why Is a No-Arg Constructor Required Here?
null a valid subtype of object ?