I see some earlier questions similar to this, but I am looking for more specific answers.
1) In this case how/when does or what makes JVM to do perform dynamic binding?
I try to call derived class method
derivedFunc1()
using super class reference variablesuperobj
. It fails (My Assumption for fail: The compiler says the memberderivedFunc1()
does not fall under the memory that is allocated(or restricted) for super class reference variable.)I try to call method
superFunc1()
using super class reference variablesuperobj
. Here inspite of havingsuperFunc1()
in super class it "somehow" callssuperFunc1()
from derived class. (This is contradicting my assumption, as accessing beyond its restricted scope of memory).
So my questions:
1) How superobj
is able access derived class member superFunc1()
but not able to access the other derived class member derivedFunc1()
.
2) Who/what makes a call for JVM to do perform dynamic binding?
Edit: The image might not be valid as I came to know "derived class object does not create super class object"
import java.util.*;
import java.text.*;
public class MyLocale {
public static void main(String[] args) {
MySuper superobj=new MyDerived();
superobj.derivedFunc1(); //Statment1: fails because it cannot call functions from derived class. (Okay I understand this part)
superobj.superFunc1(); //Statment2: has 2 options (one in super, one in derived) to call from and here it calls derived function.
}
}
class MySuper {
void superFunc1() {
System.out.println("Super");
}
}
class MyDerived extends MySuper{
void derivedFunc1() {
System.out.println("Derived");
}
void superFunc1() {
System.out.println("Derived");
}
}