jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor invocation 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 invocation" Watch "constructor invocation" New topic
Author

constructor invocation

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
can any one explain with exampleif possibleSuppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?

a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword this.
f. The keyword super.

ans: b,d,e,f


Harish<br />SCJP 1.4 (85%)
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by harish shankarnarayan:
can any one explain with exampleif possibleSuppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?

a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword this.
f. The keyword super.

ans: b,d,e,f


Hi Harish,
Lets have the example first


so here the compiler complains that:
------------------------------------
SubClass.java:15: cannot reference xx before supertype constructor has been called
super(xx,yy);//1) this gives compiler error
^
SubClass.java:15: cannot reference yy before supertype constructor has been called
super(xx,yy);//1) this gives compiler error
--------------------------------------

in the first comment 1) the variables xx and yy cannot be referenced before the superclass constructor is called coz these are instance variables ie. the variables of the object. while processing the constructor the object is not fully initilized and hence the variables xx and yy cannot be referenced.
This shows that ans B is correct.

Same is the case with instance methods. and hence ans D is also Correct.

Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Continuing the previous post:

Lets see another example:


here the compiler complains that:
---------------------------------
SubClass.java:13: cannot reference this before supertype constructor has been called
super(this);
^
1 error
----------------------------------

the error is very much similar to last errors:
'this' refers to the current object. and while the current object is still in the process of construction how can you use it?
therefore sending the reference of current object as an argument to the superclass constructor complains about this.

Hence the ans E is correct.

and for the ans F, if you try to pass the superclass object to super constructor you get following errors:
--------------------------------------
SubClass.java:13: '.' expected
super(super);
^
SubClass.java:13: <identifier> expected
super(super);
^
2 errors
------------------------------------------
i am not able to understand these errors so not sure about ans f.

Hope this clears some of your doubts

Sandy
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?

a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword this.
f. The keyword super.

ans: b,d,e,f[/B]
wenever An Object Is 2 B Created ,All Superclass Objects Are Also Created .Coz Subclass constructor contain call 2 super class constructor.Also B4 ANY Object creation ,Initialization happens for that object.
Now,if call 2 superclass is there ,then that may contain call 2 many other calls too.but that object is last 2 B Created After Compltion Of All Other superclass Objects.........so No Instance Vrbls Are Allowed.Keyword this And super are Obvs-ly Not Allowed as Arg

######################################################33
Agrah Upadhyay
B.Tech
3rd Year
 
GeeCON Prague 2014
 
subject: constructor invocation