Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor invocation

 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic