Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

problem in certpal mock ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic