Home  >  Article  >  Java  >  How to implement static and dynamic binding in java

How to implement static and dynamic binding in java

WBOY
WBOYforward
2023-06-03 18:22:031041browse

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 How to implement 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