The final modifier in Java does not provide for early binding of the method (at the compilation stage), only does not allow to determine the method in the heir with the same signature, including overriding the corresponding ancestor method, is this so?

Question: Does Java previously bind, that is, when already at the compilation stage, the method call is matched with the code to be executed?

  • Ps Does Java have early binding? - Jan Bronnikau
  • What is the earlier binding on your gentlemen programmers? - Gorets 3:49 pm
  • Already at the compilation stage, the method call is matched to the code to be executed. - Jan Bronnikau

3 answers 3

Quote from the source: Polymorphism in Java :

If overloaded methods with the same name are in the same class, the parameter lists must be different. But if the subclass method coincides with the method of the superclass (generating class), then the subclass method overrides the superclass method. Both the method names and the types of input and output parameters must match. In this case, redefinition of methods is the basis of the concept of dynamic binding (or late binding ) that implements polymorphism. The essence of dynamic dispatching methods is that the decision to call the overridden method is made at runtime, not at compile time. However, final methods are not redefinable, their call can be organized at compile time and is called early binding .

  • 2
    You can also add static methods, they also have an early binding - IronVbif
  • 2
    More method calls through the super keyword always use early binding. - Pavel Mayorov

final at its simplest, binds a variable to a value, thereby making it a constant, i.e.

  final int a = 0; // a всегда будет 0 
  • and then how is the final constant int a = 0 different? final int a = 0? - Kerins_Sataier 4:08 pm
  • 3
    The fact that the first option does not even compile, as wrong - Gorets
  • one
    I asked about the final method - it's not at all. - Jan Bronnikau
  • five
    If the method is final, then it cannot be override, i.e. Override in the classes heirs - Gorets
  • In C #, the non-virtual method is also impossible to override, however, nothing prevents the heir from implementing the same method with the same signature. - Jan Bronnikau

What about overloading?

 public class LinkageTest { public static class Parent { public void test() { System.out.println("parent::test"); } } public static class Child extends Parent { public void test() { System.out.println("child::test"); } } public static class Tester { public void test(Parent obj) { System.out.println("Testing parent..."); obj.test(); } public void test(Child obj) { System.out.println("Testing child..."); obj.test(); } } public static void main(String[] args) { Parent obj = new Child(); Tester t = new Tester(); t.test(obj); } } 

Conclusion:

 Testing parent... child::test