Juhan,
There's a few issues going on here that I think are contributing to your confusion.
The first is your choice to name your parent class "Super", which may be confusing you about the use of the keyword
super. The
Java tutorial gives
a good introduction to super and how to use it.
Second, because you're trying to experiment with a a third-generation class, Sub2, you're not going to get the behavior you expect. Sub2 inherits from Sub, which overrides Super's implementation of getI with its own.
To access a parent's class method, don't use casting, use the super keyword:
But because Sub2's super class is Sub, it will always call Sub's implementation of getI().
If you're really trying to experiment with casting, then you have to remember effect of
polymorphism and late binding -- the version of of the method that is called be called depends on the type of the object that has been created, not on the type of the reference.
To see this, I've slightly modified your code:
If you run this, you'll get the following output:
Hopefully this explanation will get you a little closer to better understanding what's going on here.