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

super and this

 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We know that either the "super" or "this" statement should be the first statement in a constructor. What should be done in case I want to invoke both of these from the same constructor?
For eg., consider the following code
<CODE>
class superclass
{
superclass()
{
System.out.println("In superclass() constructor");
}
superclass(int i)
{
System.out.println("In superclass(int) constructor");
}
superclass(String s)
{
System.out.println("In superclass(String) constructor");
}
}
class subclass extends superclass
{
subclass()
{
System.out.println("In subclass() constructor");
}
subclass(int i)
{
super(i);//problem here
this(""+i);//and problem here too!
System.out.println("In subclass(int) constructor");
}
subclass(String s)
{
super(s);
System.out.println("In subclass(String) constructor");
}
}
class superthis
{
public static void main(String args[])
{
subclass obj=new subclass(30);
}
}
</CODE>
When I create an object of the subclass with an int parameter, there occur problems in the lines mentioned in the code. Both "super" and "this" compete for being the first line of the constructor and hence a compiler error results. Is there any way I can take care of this case?
Thanks,
Aparna

 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not as far as i know. you can only have one or the other. if your superclass call was to the no args constructor, you could just leave out the call. java would take care of it. I think.
[This message has been edited by Randall Twede (edited December 12, 2000).]
 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randall,
Yeah, I know that super(), super(some args), this(), this(some args) are all mutually exclusive. But my requirement is not uncommon. What should I do when I need to call both from the same constructor? Consider the code I have posted in my question. Such a requirement can commonly arise. How can I take care of such a situation?
Thanks,
Aparna
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe(this is conjecture) you could call like this:
MyProgram() {
this(i);
}
MyProgram(int i) {
super(i);
}
get my drift?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>But my requirement is not uncommon
True. And the solution is also quite common.
Move the code from one contructor to a method. Call that method from where ever you want.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic