Following are 2 examples from Rasmussen
where in this has been typecasted
y does first one give compilation error where as the second one doesnt?
public class myclass
{
public static void main(
String args[])
{
.....
}
public static class class state
{
protected int value;
..........
class momento
{
int value=10;
momento() {this.val=state.this.val;}
void restore()
{
((state)this).val=this.val; //1
}
}
}
}
The above code gives a compilation error at 1
whereas the following doesnt at 2 and 3 WHY ?
class light
{
protected double getbill(int ..){}
protected String billtype;
.....
}
class tubelight extends light{}
class neonlight extends tubelight
{
.....
((light)this).getbill(20);//2
System.out.println((light)this).billtype);//3
}