Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Question

 
David Kennedy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im just wondering why doesnt this compile



Thanks
David
 
Max Vandenburg
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    David Kennedy
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand it now.

    Thanks for the help

    David
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic