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