This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor Question" Watch "Constructor Question" New topic
Author

Constructor Question

David Kennedy
Ranch Hand

Joined: Jan 22, 2006
Posts: 33
Hi,

Im just wondering why doesnt this compile



Thanks
David


"There are only 10 types of people in the world: Those who understand binary, and those who don't"
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
its because the constructor in the Top class takes an argument string....

for it to work, either explicitly add super("someString"); to your Bottom2() constructor i.e.



or create an constructor in the Top class that takes no arguments.


MV
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Like Firman said. Remember...
  • If there is no explicit call to this(...) or super(...), the first line of a constructor is an implicit call to super() with no arguments.
  • A default no-args constructor will be automatically provided by the compiler only if no other constructor is provided by the programmer.
  • In this case, a Top constructor accepting a String is provided by the programmer, so the compiler does not provide a no-args constructor for Top. The constructor for the subclass, Bottom2, does not call super explicitly, and so super() with no arguments is implicitly called. This is a problem, because there is no such constructor.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    David Kennedy
    Ranch Hand

    Joined: Jan 22, 2006
    Posts: 33
    I understand it now.

    Thanks for the help

    David
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Constructor Question