This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
 
Similar Threads
Initializer doubt
Var-arg class
Increment on object reference ?
Compile time polymorphism
Question about the "Getting in Touch with your Inner Class" campfire tutorial