Virtual Function Optimization

Java provides dynamic method dispatch. Dynamic method dispatch is the basis for Runtime polymorphism. This is also known as Late Binding and is quite timeconsuming as the resolution of the function call is deffered till the function is actually called during the runtime. Thus it is advantageous to convert virtual calls to non-virtual calls whenever possible.

Example:

In the code fragement below, the call to the virtual function can be resolved at compile time.


      class TestClass {
            public void VirtualGetOne() { return ; }
            public final void GetOne() { return ; }
      }

      class myclass {
            public void myfunction() {
                   TestClass c1 = new TestClass() ;
		   c1.VirtualGetOne() ;
            }
      }

  

Below is the code fragment after Virtual Function Optimization.


      class TestClass {
            public void VirtualGetOne() { return ; }
            public final void GetOne() { return ; }
      }
 
      class myclass {
            public void myfunction() {
                   TestClass c1 = new TestClass() ;
                   c1.GetOne() ;
            }
      }