• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Constructor

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Trailboss
Posts: 23615
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No apology necessary!
reply
    Bookmark Topic Watch Topic
  • New Topic