0

I read a few questions related to this but this not clear on why do we have runtime polymorphism at all. Why we actually need runtime polymorphism? What exactly does runtime polymorphism mean? I am somehow still not clear why this was done.

/* All Public Class's in real java program are in separate files */
public class ParentClass {
    public TestJava testJavaObj;
    public void parentMethod(){ }
    public static void parentStaticMethod () { }
}

public class ChildClass extends ParentClass {
    public TestJava testJavaObj;
    @Override public void parentMethod() { }
    public void childMethod() { }
}

public class GrandChildClass extends ChildClass {
    public TestJava testJavaObj;
    @Override public void parentMethod() { }
    @Override public void childMethod() { }
    public void grandChildMethod() { }
}

public class TestJava {
    public static void main(String[] args) {
        ParentClass familyTreeObj;

        // 1st Scenario
        familyTreeObj = new GrandChildClass();
        familyTreeObj.parentMethod();
        ((ChildClass)familyTreeObj).childMethod();
        ((GrandChildClass)familyTreeObj).grandChildMethod();

        // 2nd Scenario
        if (new Random().nextBoolean()) {
            familyTreeObj = new GrandChildClass();
        } else {
            familyTreeObj = new ChildClass();
        }
        familyTreeObj.parentMethod();
        ((ChildClass)familyTreeObj).childMethod();
        ((GrandChildClass)familyTreeObj).grandChildMethod();
    }
}

The way I understand the above :

That a memory object is created and "familyTreeObj" holds the reference to that memory location. Now with that memory location there is a

#Memory Object - Holding reference to TestJava's Actual memory Object.
#Memory Ref to function - parentMethod implementation overridden by GrandChildClass
#Memory Ref to function - childMethod implementation overridden by GrandChildClass
#Memory Ref to function - grandChildMethod implementation overridden by GrandChildClass

The static method in parent class is like a Global method and has its own memory location (for sub routine when compiled to machine level code). And from the Java language perspective thus they are specific to the Class, referenced through Class context - ParentClass.parentStaticMethod() from any of the class methods. And thus there implementation can not be overriden.

But when I create this ParentClass familyTreeObj = new GrandChildClass();

I can only access memory locations accessible by scope of Parent class object because of upcasting.

Everywhere I see scenario 1 shown as Runtime Polymorphism example. I know clearly that which method implementation will be called and so can the compiler then why is it Runtime Polymorphism. Since having runtime decision will increase the running time of the program.
Where as scenario 2 is actually requires a Runtime decision. In fact I cannot imagine any scenario where method overriding is a Runtime Polymorphism other than the one shown in scenario 2 or where we take some input (which cannot be predicted) and base our object initialization on that.

I understand that this optimization has been left to compiler. My question is why ?

Also was there any logic of not allowing member fields to be overridden ?

Heisenberg
  • 26
  • 3

0 Answers0