Home  >  Article  >  Java  >  Comparative analysis of examples of static and dynamic binding in java

Comparative analysis of examples of static and dynamic binding in java

WBOY
WBOYforward
2023-05-02 23:04:05757browse

Comparison of different bindings

1. Static binding occurs during compilation, and dynamic binding occurs during runtime.

2. Dynamic binding is more flexible than static binding, because static binding is determined during the compilation process, while dynamic binding does not know which method to call during the compilation process.

3. Static binding calls methods faster than dynamic binding, because static binding can be called directly, while dynamic binding needs to search the method table.

Example

Static binding

class Super{
public static void sample(){
System.out.println("This is the method of super class");
 
}
 
}
 
Public class Sub extends Super{
Public static void sample(){
System.out.println("This is the method of sub class");
 
}
 
Public static void main(String args[]){
Sub.sample()
 
}
 
}

(2)Dynamic binding

class Super{
public void sample(){
System.out.println("This is the method of super class");
 
}
 
}
 
Public class extends Super{
Public static void sample(){
System.out.println("This is the method of sub class");
 
}
 
Public static void main(String args[]){
new Sub().sample()
 
}
 
}

The above is the detailed content of Comparative analysis of examples of static and dynamic binding in java. For more information, please follow other related articles on the PHP Chinese website!

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