aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor 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" Watch "Constructor" New topic
Author

Constructor

RajeshParab
Greenhorn

Joined: Feb 08, 2000
Posts: 14
What will be the output when you compile and execute the following program.

////////////////////////////////////////////

class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

Child(int i) { test (); }

Child(float f) { this ((int)f); }

void test() {
System.out.println("Child.test()");
}

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???
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20693
    ∞

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?

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Sushma
Ranch Hand

Joined: Feb 24, 2000
Posts: 36
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: 18671
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
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: 18671
No apology necessary!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor