Your variable i is by default inherited from super. Since you declare it again, you are shadowing the declarion in the super class. How coud you distinguish the varible i declared in the super class than that declared in the child class?

Well, it is resolved pretty simple. When the variable is shadowed, in order to decide which of the two you want to access, the runtime will give you the value of the variable that corresponds to the type of the object that you are using to access it.

If you cast your object to a Sub type then you will get the value of the i variable declared in the subclass.