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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor problem" Watch "Constructor problem" New topic
Author

Constructor problem

Venkat01
Greenhorn

Joined: Jan 09, 2000
Posts: 24
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
"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?

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Rajiv Ahuja
Greenhorn

Joined: Feb 02, 2000
Posts: 19
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Feb 09, 2000
Posts: 53
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

Joined: Aug 29, 2001
Posts: 219
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.


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
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


best regards,<br />Steven<br />SCJP, SCEA
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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!


Rob
SCJP 1.4
Kasey Tritt
Greenhorn

Joined: May 02, 2002
Posts: 15

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

Joined: Jan 07, 2002
Posts: 2205
Very well stated Kasey, I agree with your logic.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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...


SCJP Tipline, etc.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
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

Joined: Jan 07, 2002
Posts: 2205
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor problem
 
Similar Threads
Khalid Mock Exam
Exam question - constructors- help!
Constructor in parent class
constructor
constructor question