class P
{
void speak()
{
print ("Hello P");
}
}
class C extends P
{
void speak(String s)
{
print ("Hello C");
}
}
// And then somewhere…
public static void main()
{
P ref = new C();
ref.speak();
}
Please do correct where I'm wrong. When the code compiles, the compiler first checks the class that ref belongs to see if it contains a speak (). Once confirmed, it sees that the speak() is virtual so it won't statically bind it. During runtime, as per logic the Jvm would see that ref points to a C class object so it should call speak () of C class but instead it follows the rule of the statically typed and calls the method of P class. How does the complier know that these 2 methods are overloaded when they are in different classes and therefore have to be statically binded?