When you create an object of type Sub, it also creates an object of type Super, since Sub extends Super.
So you do new Sub(), what java does is looks at what Sub is, sees it extends Super, so before it does anything with a Sub, it creates a Super, and so creates a foo. Then it looks at Sub itself, and creates another foo.
Now, when you cast sub as a Super, you're basicly telling java to ignore the fact that it's actually a Sub, and treat it as a Super, and so it does, it gets Super's variables, methods etc, and ignores anything you've re-defined in Sub.
If you created identically named methods in both Sub and Super, then whichever one got used woudl depend on what the object was cast as at the time. You can't cast it to a Super, and expect to access the Sub.
e.g.
Code:class Super{ int foo=1; }
class Sub extends Super { int bar=2; }
Sub a=new Sub();
println(a.foo); //1
println(a.bar); //2
Super b=(Super)a;
println(b.foo); //1
println(b.bar); // ERROR Super doens't have a variable called bar