Hello, Overriding only concerns instance methods. It has nothing to do with static methods or instance variables. When writing ((C18)this).s1 in D18, polymorphism doesn't worh here. It just allows you to access the "C18 part" of D18 with the cast.
However, when writting ((C20)this).m1(); polymorphism works and the JVM pick the m1 method according to the object type and not the reference type. In that case, the object type referenced by this is D20 so the JVM choose the m1 method from D20. The cast has no effect in that case.
Hi, For variables, there is no operator overloading and overiding in java. based on refference variable only it will invoke.
For methods, we have mehod overloading and riding. when we are invoking a method the jvm will do the following things 1)If it is overrridden method, then it will invoke the based on runtime object but not refference type. 2)If it is not overridden method , then it will invoke based on refference type.