Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d
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);
}

this(x,y) need going first.


a good code its here




second class


output
Created Parent
Creating child
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried a little Google for fun, and found the following:
http://www.javabeat.net/javabeat/scjp2/mocks/scjp_1_4_mock_exam_questions_7.php

Question 12, same as yours. And only one answer, the third one
 
Bunty Naagar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic