On overloading, rewriting, polymorphism, static binding and dynamic binding in Java
This paper mainly studies the related contents of overloading, rewriting, polymorphism, static binding and dynamic binding in Java, as follows.
Overload, whose English name is overload, means that more than one method with the same name is defined in a class. The number, type and order of parameters of these methods cannot be the same. The return type can be the same or different.
Override, the English name is override, which means that in the case of inheritance, if a subclass defines a method with the same name, the same return type or compatible type and the same parameters as the method in its base class, it is called a subclass to override the method of the base class. This is a necessary step to achieve polymorphism.
Polymorphism: polymorphism is the ability of the same behavior to have multiple different forms or forms.
Concept of program binding:
Binding means that the call of a method is associated with the class (method body) where the method is located. For Java, binding is divided into static binding and dynamic binding; Or early binding and late binding.
Static binding:
Before the program execution, the method has been bound, which is implemented by the compiler or other linker. For example: C.
Simple binding for Java can be understood as binding during program compilation; In particular, only final, static, private and construction methods in Java are pre bound.
Dynamic binding:
Late binding: dynamic binding means that the compiler does not know which method to call at the compilation stage until the runtime binds according to the type of specific object.
If a language implements late binding, it must also provide some mechanisms to judge the type of object during operation and call appropriate methods respectively. In other words, the compiler still does not know the type of object at this time, but the method calling mechanism can investigate and find the correct method body. Different languages have different implementation methods for late binding. But we can at least think of it this way: they all need to put some special types of information in the object.
Method overloading includes static method overloading and ordinary method overloading. Static method overloading is static binding, and method calling is through: class name method. Common method overloading is dynamic binding, and method invocation is through instance object reference method. Constructors can be overloaded, but they cannot be overridden.
Static methods can be overridden, but do not achieve polymorphism.
summary
The above is all about overloading, polymorphism, static binding and dynamic binding in Java. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!