To make things clear, I have elaborated my question in more detail.
I have this code here which elaborates about dynamic binding using the concept of overriding.
Here's the code:
class Test {
public static void main(String [] args)
{
B b = new A();
b.p(10); // 10.0
b.p(10.0); // 10.0
}
}
class B
{
public void p(double i)
{
print(i*2);
}
}
class A extends B
{
public void p(double i)
{
print(i);
}
}
Now, the explanation says that the compiler cannot determine which method to call during"compile time". It is during the "run time" when the compiler knows that the method that needs to be called is the subclass method because when we override, we are looking at the actual type.
Contrast with this code :
class Test {
public static void main(String [] args)
{
B b = new A();
b.p(10); // 10.0
b.p(10.0); // 10.0
}
}
class B
{
public void p(double i)()
{
print(i*2);
}
}
class A extends B
{
public void p(int i)
{
print(i);
}
}
In this example, the compiler can recognize which method to call during compile time. How can a compile recognize in this example while fail to recognize int he previous example? Question:
What exactly does the term "compile time" and "run time" mean? And how does a compiler not recognize during compile time that the function that needs to be called is the subclass function?