• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This question is straight from the KS&BB book.



I don't quite understand how the compilation fails as explained by the author. In Bottom2 the super() would be called, which would be a no-arg constructor to its parent class Top. But since Bottom2's constructor is already overridden with arguments matching the super class constructor there shouldn't be a problem with compilation at least. Correct me if i am wrong here please..
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friend, remember that if you dont write an explicit call to the super constructor with some arguments the compiler try to insert at he first line of the constructor a super no arg call, then reviewing the parent class there is not a no arg constructor so the compilaton process fail

got it?
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add "super(s);' as the first line of Bottom2's constructor and it will compile fine.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in the K&B book they recommend to always create the default constructor if you are overloading it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic