-2

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?

Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28
  • 1) You probably meant: `class1 obj = new class2();` and 2) your question is unclear (to me). – Nir Alfasi Jul 15 '15 at 06:28
  • class1 obj = new class2() and class2 obj = new class2() can be used in the same context? @alfasin – Parveez Ahmed Jul 15 '15 at 06:32
  • Please improve your question. For one, this is not valid Java code. Also, you are making assumptions that you *think* the readers will understand such as "without instantiation (after creating an object)", and it's not clear what you're assuming here (creating an object *is* instantiation), and mentioning an interpreter (the Java runtime is *not* an interpreter). So explain the concepts that you refer to and give links to articles that define them. – RealSkeptic Jul 15 '15 at 07:01
  • `fun()` is not a class, it is a method. There are not parent or child methods but they are methods of parent and child classes. And the class names should start with uppercase letter by convention. – aalku Jul 15 '15 at 07:55
  • i meant its a method in d parent class indeed.. – Parveez Ahmed Jul 15 '15 at 08:23

3 Answers3

1

Java is not only interpreter. Modern JVMs include a very sophisticated JIT-compiler which is capable to devirtualize many calls including the case you are speaking about. When your program starts, it will be executed by interpreter which will do the virtual call. But if your main method is executed many times, it will be compiled to native code by JIT compiler. This particular case is very simple, so JIT compiler will be able to replace virtual call with direct call and even inline the fun body into the main method. HotSpot JIT compiler can even optimize the cases where exact target method is unknown, but in the most of the cases the same method was called during the profiling phase (first several thousand calls). It will inline the most common case and keep the virtual call for the less common.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
1

A compiler must obey the language spec, and language specs can differ even between languages as similar as C# and java.

If I'm not mistaken, C# defaults to binding methods to the declared type of an object, and if you want the "real" run-time binding then you need to declare methods virtual. Java specifies that C#'s "virtual" should always be the behaviour for instance (non-static) methods. Such differences in language spec cause differences in what a compiler can/cannot or should/should not do when compiling.

In this light, it's a bit unclear what your actual question is ...

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • 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? – Parveez Ahmed Jul 15 '15 at 06:57
  • In the code in your particular example, the compiler _could_ know "which fun() is called" because the instantiation of the object happens in the same method using "new ...", and because it is also the case that the called method has an implementation within the same class. So the type of the object is known, the called code is known, and since objects cannot change type, the compiler _could_ choose to resolve the call at compile time. Or even inline the called code altogether. Whether it does so or not, is for the compiler writers to decide. – Erwin Smout Jul 15 '15 at 07:06
0

In java the method you are calling is decided at compile time, not by the type of the object but by the type of the variable (unless typecast). obj is a variable of type class2 so you are calling the fun method of that class. You can read this question for more info. Java overloading method selection

Community
  • 1
  • 1
aalku
  • 2,860
  • 2
  • 23
  • 44