• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor problem

 
Venkat01
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which statements concerning the following code are true?
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
Select all valid answers:
A) The code will fail to compile.
B) The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or C.
C)class C has three constructors.
D)At most one of the constructors of each class is called as a result of constructing an object of class C
Can anyone please explain this? This has been discussed earlier in javacert, but i did not get any concrete reply and hence is reposting the same here. Thanking everyone in advance.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Can anyone please explain this?"
Explain what exactly?

"This has been discussed earlier in javacert, but i did not get any concrete reply and hence is reposting the same here. Thanking everyone in advance."
What is your answer and why do you think it's right?
 
Rajiv Ahuja
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Incorrect 'cuz compiler compiles it
2. Correct 'cuz there is no construtor in B, so compiler inserts a default constructor super(), which calls public A() of A. No need to look at C 'cuz it has to go through B constructors to call A constructors.
3.Correct 'cuz I just counted them.
4. Incorrect as 'this()' makes you call more than one constructor of same class.
This is an explanation from a beginner so experts please verify and give your blessing to my answers

[This message has been edited by Rajiv Ahuja (edited February 05, 2000).]
[This message has been edited by Rajiv Ahuja (edited February 05, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had an idea in mind that functions with the same name as class are called constructors and constructors can not return any thing. To my knowledge this was compliation error at the constructor of class B. But it is working fine and giving results.
How is it possible. Please explain.
Thanx
------------------
 
Lucy C
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had an idea in mind that functions with the same name as class are called constructors and
constructors can not return any thing. To my knowledge this was compliation error at the constructor
of class B. But it is working fine and giving results.

This came up in another thread - what you have in class B may have the same name as the class, but it has a return type, boolean, which means it's not a constructor, it's a method with the same name as the class (confusing, but perfectly legal). Constructors can't have return types.
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think answer B,C and D are correct.
Whenever an Object C is constructed, it will call all the constructors upto the hierarchy like C Constructor will call B Constructor and B Constructor will call A Constructor. B doesn't have constructor here. It is valid to declare a name of a class to be a method. Remember that, if you don't provide a constructor, Java will provide no arg constructor. In this case, Java compiler will insert B consructor which does nothing here. Lets look in detail by modifying A constructor as
public A() {
System.out.println("Constructor A called");
}

1) private C()
Constructor can be declared private. When you call new C() from main method, C will call B's default no arg constructor(compiler default) and B in turn will call A constructor. So "Constructor A called" will be printed. Note that super() is not necessary
2)public C(String msg) { this(); }
when you call new C("I am C") from main method, C's no arg constructor will be called because of this(). C no arg default constructor calls B's default constructor and in turn A constructor will be called. So "Constructor A called" will be printed.
3) Again, B's constructor gets called and "Constructor A called" will be printed.
Hope this helps.
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
D)At most one of the constructors of each class is called as a result of constructing an object of class C
If I construct an object of Class C through the overloaded constructor,
public C(String msg) { this(); }
I would have invoked two constructors of class C.
Thus the above statement cannot be correct.
Someone correct me if I am wrong.

clement
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy Mackeral, a triple whammy!!

Venkat01 , neetugupta , and Lucy C:
Please change your name to comply with the naming policy to which you agreed when you registered here..

For your publicly displayed name,
use a first name, a space, and a last name.

You can change your name:
here

You can also find the naming policy:
here
Thank You!
 
Kasey Tritt
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajiv Ahuja

1. Incorrect 'cuz compiler compiles it
2. Correct 'cuz there is no construtor in B, so compiler inserts a default constructor super(), which calls public A() of A. No need to look at C 'cuz it has to go through B constructors to call A constructors.
3.Correct 'cuz I just counted them.
4. Incorrect as 'this()' makes you call more than one constructor of same class.
This is an explanation from a beginner so experts please verify and give your blessing to my answers

I agree with your answers, the valid answers are B and C. I think you have explained it pretty well.
Clement Ng

If I construct an object of Class C through the overloaded constructor,
public C(String msg) { this(); }
I would have invoked two constructors of class C.
Thus the above statement cannot be correct.

Thiru Thangavelu
I think answer B,C and D are correct.

D is wrong. As explained by the above 2 gentlemen, constructing object C through the constructor public C(String msg) { this(); } will result in invoking 2 constructors of C. Thus it will be incorrect to say at most one of the constructors of each class is called as a result of constructing an object of class C.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very well stated Kasey, I agree with your logic.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
Holy Mackeral, a triple whammy!!

Too bad those posts are from 2 years ago...somehow, I doubt those names will get changed...
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you guys, I interpretted as AT LEAST one constructor from each class will be interpreted.Sorry and thanks for the advice.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Corey McGlone:

Too bad those posts are from 2 years ago...somehow, I doubt those names will get changed...

DOH.
I hate when that happens! :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic