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