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

How to implement static and dynamic binding in java

WBOY
Release: 2023-06-03 18:22:03
forward
1074 people have browsed it

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()
 
}
 
}
Copy after login

(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()
 
}
 
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template