0

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?

Stranger
  • 864
  • 4
  • 21
  • 48

9 Answers9

5

Consider the following (pseudo)code:

B b = Rand() > 0.5?new A() : new B();

b.p(10);

There is no way the compiler can know which will be called at runtime.

AlexDev
  • 4,049
  • 31
  • 36
3

It's pretty self-explanitory

Compile-time and run-time refer to a period in time


Compile time is the time when your compiler is building your project.

Runtime is the time when your project is running.


And i'm presuming you're asking about why your code doesn't know that B is an A before the code runs. in the following section:

 B b = new A();   

 b.p(10);      //  20.0
 b.p(10.0);   // 20.0

The reason is because the compiler doesn't evaluate every possible path in the code to check for that stuff, This is easier to understand when you see slightly more complex implementations.

 B b = new A();   

 if(...)
 {
    ...
    b = new B();
 }


 b.p(10);      //  20.0
 b.p(10.0);   // 20.0

It only knows what b is when the code is actually executing.

2

Run time means "when your program is running".

Compile time means "when your program is being compiled".

There are certain things that are only known at run time.

An example would be user input.

A compiler cannot anticipate what users will input at run time.

jahroy
  • 22,322
  • 9
  • 59
  • 108
2

What exactly does the term "compile time" and "run time" mean?

Compile time is when the code gets compiled to executable code (byte-code). That means, all files are linked (which you include by import expression), and byte-code, i.e. sequence of instructions is created.

Run time is when the CPU runs the compiled code - byte code.

And how does a compiler not recognize during compile time that the function that needs to be called is the subclass function?

Actually, it does. If it cannot recognize then it will not compile because compile must know unambiguously know what method to call in order to prepare the code for run time. The code you provided is valid, and the p() method from class A will be executed.

darijan
  • 9,725
  • 25
  • 38
1
  • Compile time: during compilation.
  • Run time: during program execution.

Consider:

List x = getsSomeList();

x could be any implementation of the List interface.

If you call x.add(foo), the correct add method won't be known until execution and there's an actual implementation on which to call add.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

The compiler can't know which method call when you're using polymorphism, because you can have many implementations of the same method... because of that it can only be determined at runtime.

DGomez
  • 1,450
  • 9
  • 25
0

The difference between "compile time" and "runtime" is during the compiling, the JVM checks for syntax, specific Java, and other errors in the program that the programmer writes. The runtime is what is checked during the running of the application.

There is a possibility that you can be syntaxly correct, but have many errors in the runtime. Examples: mispelled words logic in the program the way it runs how things work dealing with animations etc

It depe

user2277872
  • 2,963
  • 1
  • 21
  • 22
0

The compiler cannot know which method to call because of Polymorphism, As said that the compiler cannot determine which method to call during"compile time" it meant that since multiple form of the function is available, the compiler decides which version of function it will call when the application is being compiled, so instead it decide which method to call when the program is being executed run-time).

If the extended/derived class overloads the function/method that you are trying to call it call the method from the derived class, if the method is not overloaded in derived class, then it will call the method from base class. That is how dynamic binding works in OOP oriented languages.

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
-2

It looks like the example you have provided is incorrect.

The output is 10.0 in both invocations, not 20.0 as you have stated.

JamesB
  • 7,774
  • 2
  • 22
  • 21