File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor question" Watch "Constructor question" New topic

Constructor question

Sumanth Shanbhag

Joined: May 10, 2007
Posts: 13

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..
Mercurio Savedra

Joined: Dec 04, 2005
Posts: 25
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?
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Add "super(s);' as the first line of Bottom2's constructor and it will compile fine.
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
I think in the K&B book they recommend to always create the default constructor if you are overloading it.

Scooby Snacks for everyone...<br /> <br />SCJA, SCJP 1.4
I agree. Here's the link:
subject: Constructor question
It's not a secret anymore!