• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Constructor Overloading

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will appear in the standard output when you run the Tester class?
1. class Tester {
2. int var;
3. Tester(double var) {
4. this.var = (int)var;
5. }
6. Tester(int var) {
7. this("hello");
8. }
9. Tester(String s) {
10. this();
11. System.out.println(s);
12. }
13. Tester() {
14. System.out.println("good-bye");
15. }
16. public static void main(String args[]) {
17. Tester t = new Tester(5);
18. }

Ans :
a. "hello"
b. 5
c. "hello" followed by "good-bye"
d. "good-bye" followed by "hello"
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is no.4.
Follow me on this one. You create an instance of Tester with an argument of 5. We look in the class Tester for the appropriate constructor. We find a constructor that takes an int as an argument " Tester (int i) " Inside that constructor, there is a call to the constructor Tester (String s) " with the implicit this("hello"). Inside that constructor is a call to the no args constructor this(). Inside the no args constructor the String "good bye" is printed out. Then we return to the Tester(String s) constructor which prints out the string that is an argument which is "hello" thus you get "good bye" and hello" at the output. I know that this may seem a bit daunting, but just keep in mind that constructors aren't inherited, they're chained.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudipta Das:
What will appear in the standard output when you run the Tester class?
1. class Tester {
2. int var;
3. Tester(double var) {
4. this.var = (int)var;
5. }
6. Tester(int var) {
7. this("hello");
8. }
9. Tester(String s) {
10. this();
11. System.out.println(s);
12. }
13. Tester() {
14. System.out.println("good-bye");
15. }
16. public static void main(String args[]) {
17. Tester t = new Tester(5);
18. }

Ans :
a. "hello"
b. 5
c. "hello" followed by "good-bye"
d. "good-bye" followed by "hello"



1.Tester t=new Tester(5) invokes the constructor Tester(int var) of the class Tester as 5 is an integer.
2.Tester(5) invokes the constructor Tester(String s) where s="hello" with the call this("hello") .
3.Tester("hello") invokes the parameterless constructor Tester() which prints "good-bye" and then control returns to Tester("hello") which prints "hello" in the next line.
4.control then returns to Tester(5) and then back to main.
Hence the ouptut is:"good-bye" followed by "hello"

[This message has been edited by sai challa (edited April 19, 2001).]
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic