• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
RajeshParab
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21684
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sushma
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the test method called again after the child object is created???
What is taken as the default constructor???
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Sushma
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm really sorry Jim, i didn't see that the instation has called test().
Thank you
Sushma
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No apology necessary!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic