jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor" Watch "constructor" New topic


Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481

What will happen if you try to compile and run above program?

.a It will compile successfully. It will output "Created Parent" and then "Creating child"

b. It will compile successfully. It will output "Creating child" and then "Created Parent"

c. It will not compile giving warning, "Explicit constructor invocation must be first statement in constructor."

d. It will not compile giving warning, "Expression is not a valid block statement."

Answers are c and d.I know c is the correct answer and what is the meaning of option d.i don't get the meaning?
please help on this.
Line 1 and line2 will give compile error since Superclass didn't have no-arg constructor.But which Expression is not a valid block statement.
marcos rodrigues

Joined: Mar 08, 2007
Posts: 7
public Child(int x, int y){//line1


dont have super();

dont have a default constructor

public Child(int x, int y, int z){//line2
System.out.println("Creating child");

this(x,y) need going first.

a good code its here

second class

Created Parent
Creating child
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

I've tried a little Google for fun, and found the following:

Question 12, same as yours. And only one answer, the third one

[My Blog]
All roads lead to JavaRanch
Bunty Naagar

Joined: Feb 27, 2007
Posts: 14
If you compile this, you will find only option (c) is the correct answer. Also you get following errors , but that is not mentioned in any options. This error is because of absence of a no argument constructor for parent & the fact that child constructor does not calls the super constructor with arguments

cannot find symbol
symbol : constructor Parent()
location: class Parent
[ March 09, 2007: Message edited by: Bunty Naagar ]

I agree. Here's the link: http://aspose.com/file-tools
subject: constructor
It's not a secret anymore!