aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 19672
    ∞

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 forums
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: 18670
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: 18670
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.
 
subject: Constructor
 
Similar Threads
Confused help me!!
Jargon mock exam
super in constructors
mock exam Jargon(sarga)
java programmer certificate