aspose file tools*
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
Author

constructors

antraarora
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.
Thanks,
Antra
What is the result of attempting to compile and run the following code ?
class mainClass {
mainClass(int i) {
System.out.println("mainClass(int)");
}
}
class TestMe extends mainClass {
TestMe() {
System.out.println("TestMe()");
}
TestMe(int i) {
System.out.println("TestMe(int)");
}
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
Greenhorn

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
Anand

[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).]


Cheers,<BR>Anand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors