The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructors 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 "constructors" Watch "constructors" New topic


Ranch Hand

Joined: Jul 27, 2000
Posts: 45
Now this is one topic which gets me every time.
Could somebody help me do this and also explain why they chose that option.
What is the result of attempting to compile and run the following code ?
class mainClass {
mainClass(int i) {
class TestMe extends mainClass {
TestMe() {
TestMe(int i) {
public static void main(String args[]) {
TestMe tm = new TestMe(4);
A. mainClass(int)
B. TestMe()
C. TestMe(int)
D. Compile Time error � no sonstructor called mainClass() found in mainClass
Anand M S

Joined: Feb 22, 2000
Posts: 14
Hi Antra,
Answer is D,when we create the sub class object, first the base class default
no-argument constructor will be called, but here in base class (mainClass) there is no default no-argument constructor. U have provided the constructor "mainClass(int i)", so the no-argument constructor will not be added automatically. If u remove the constructor(mainClass(int i)) from base class, then default no-argument constructor will be added autoamtically and it works fine, other wise we need to provide
no-argument default constructor as well.
I hope this will help u

[This message has been edited by Anand M S (edited August 24, 2000).]
[This message has been edited by Anand M S (edited August 24, 2000).]

I agree. Here's the link:
subject: constructors
It's not a secret anymore!