static public void main(String[] a) { new Child(10.8f).test(); } } Select most appropriate answer.
a) Child.test() //true Child.test() b) Compilation Error: No default constructor ( constructor matching Base()) found in class Base. c) Runtime Error: No default constructor ( constructor matching Base()) found in class Base. d) Compilation Error: Cannot call this() from a constructor. I don't know how we get this output. help me here???
The answer is A. main() creates a new child object calling the constructor that takes a float. That constructor calls the constructor that takes an int, which calls the test() method. Since this is a Child object, Child.test() is the one that is called. Immediately after the object is created, the test() method is called directly. The result is that "Child.test()" appears on the screen twice. B and C are wrong because there is a default constructor in Base - there is no constructor specified, therefore the default constructor is used! D is wrong because where else are you going to call other constructors if not from a constructor?
How is the test method called again after the child object is created??? What is taken as the default constructor???
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Sushma- test() is called again after the constructor because the code says to do so: <code><pre> new Child(10.8f).test();</pre></code> If you just wanted to call the constructor (which will still call test() itself, once), then use this instead: <code><pre> new Child(10.8f);</pre></code> The default constructor is a no-argument constructor - since you provided no constructors for Base, the compiler pretends that you wrote this one: <code><pre> public Base() {}</pre><code>
[This message has been edited by Jim Yingst (edited February 28, 2000).]
"I'm not back." - Bill Harding, Twister
Sushma
Ranch Hand
Joined: Feb 24, 2000
Posts: 36
posted
0
i'm really sorry Jim, i didn't see that the instation has called test(). Thank you Sushma
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
No apology necessary!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.