super concept without confusion
=======================================
2 rules to know where the method will be executed (when a super-type reference is used to call methods)
NOTE: check the “create object” / “link assignment” statement to apply the rule
1 RULE: 1st check the method to be called. If it is static / overloaded / single - then it becomes static polymorphism / static (the compiler is looking for a reference type) --- hence it always runs from a reference type
2: The correct verification method: if it is overridden , then it becomes a dynamic polymorphism (jvm searches for the type of the object) --- therefore, it is always executed from the type of the object (i.e. the right to a new keyword)
eg:
super s=new child(); s.play();
there are 2 cases:
1st : check play () is what I set (static / overloaded / single method) or dynamic (overridden)
2nd: if static it will be executed from super ie reference type leads to polymorphism of compilation time
if dynamic it will be executed from a child type ie, leads to dynamic polymorphism
source share