A sample code segment in java where the parent class fun()
is overridden by the child class fun()
:
class class1
{
//members
void fun()
{
//some code
}
}
class2 extends class1
{
//members
void fun()
{
//some code
}
}
class overriding
{
public static void main(String args[])
{
class2 obj = new class2();
obj.fun();
}
}
In the above code, as we know that the binding of the actual code associated with the function call(obj.fun())
is done during run-time by the interpreter. During compilation the compiler is unable to bind the actual code with the call.
My question is:
Without instantiation(after creating an object of a class) does an ideal compiler have no way to know which function is to be invoked with response to a function call? Or it is done the way(as it is in java, for example) that dynamic binding has an edge over static binding in any programming paradigm?
My question is generalized.With regard to the code above,does the compiler has no way at all to know actually which fun() is called in the obj.fun() statement or technically run time binding is invented because it is impossible to bind at compile time?