a sample code segment in java parent class fun() overridden 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 above code, know binding of actual code associated function call(obj.fun()) done during run-time interpreter. during compilation compiler unable bind actual code call.
my question is:
without instantiation(after creating object of class) ideal compiler have no way know function invoked response function call? or done way(as in java, example) dynamic binding has edge on static binding in programming paradigm?
my question generalized.with regard code above,does compiler has no way @ know fun() called in obj.fun() statement or technically run time binding invented because impossible bind @ compile time?
java not interpreter. modern jvms include sophisticated jit-compiler capable devirtualize many calls including case speaking about. when program starts, executed interpreter virtual call. if main method executed many times, compiled native code jit compiler. particular case simple, jit compiler able replace virtual call direct call , inline fun body main method. hotspot jit compiler can optimize cases exact target method unknown, in of cases same method called during profiling phase (first several thousand calls). inline common case , keep virtual call less common.
Comments
Post a Comment