Home  >  Article  >  Java  >  The difference between static binding and dynamic binding in Java

The difference between static binding and dynamic binding in Java

WBOY
WBOYforward
2023-08-27 23:09:081359browse

The difference between static binding and dynamic binding in Java

#Binding is a mechanism that creates a link between a method call and the actual implementation of the method. According to the concept of polymorphism in Java, objects can have many different forms. The object form can be resolved at compile time and run time. If the link between method invocation and method implementation is resolved at compile time, we call it static binding; if it is resolved at runtime, we call it dynamic binding. Dynamic binding uses objects to resolve bindings, while static binding uses classes and fields' types.

##Dynamic binding uses objects to resolve the binding3ExampleOverload It is an example of static bindingMethod overriding is an example of dynamic binding4.Method typePrivate, final Use static binding for static methods and variablesUse dynamic binding for virtual methodsStatic and dynamic binding examples
public class FastFood {
   public void create() {
      System.out.println("Creating in FastFood class");
   }
}
public class Pizza extends FastFood {
   public void create() {
      System.out.println("Creating in Pizza class");
   }
}
public class Main {
   public static void main(String[] args) {
      FastFood fastFood= new FastFood();
      fastFood.create();
      //Dynamic binding
      FastFood pza= new Pizza();
      pza.create();
   }
}
Old gentleman. no.

Key

Static binding

Dynamic binding

##1 p>

##Basic

Resolved at compile time

Resolved at runtime

2

Parsing mechanism

Static binding uses the types of classes and fields

td>

The above is the detailed content of The difference between static binding and dynamic binding in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete