This article mainly studies related content about overloading, rewriting, polymorphism, static binding and dynamic binding in Java, as follows.
Overload , the English name is overload, which means that more than one method with the same name is defined in a class. The number of parameters, parameter types and order of these methods cannot be the same. The return type can be the same or different.
public class TstaticOverload {static int height;TstaticOverload() {System.out.println ("Planting a seedling");height = 0;}TstaticOverload(int initialHeight) {height = initialHeight;System.out.println("Creating new Tree that is "+ height + " feet tall");}static void info() {System.out.println("Tree is " + height + " feet tall");}static void info(String s) {System.out.println(s + ": Tree is " + height + " feet tall");}} public class testSO {public static void main (String [] args) {TstaticOverload t = new TstaticOverload(5);TstaticOverload.info();TstaticOverload.info("overloading method");new TstaticOverload();}}out: Creating new Tree that is 5 feet tall Tree is 5 feet tall overloading method: Tree is 5 feet tall Planting a seedling
Rewrite, the English name is override, which means that in the case of inheritance, a method defined in the subclass that has the same name, the same return type, or compatible type and the same parameters as the methods in the base class is defined. This is called the subclass rewrites the base class method. This is a necessary step to implement polymorphism.
Polymorphism: A polymorphism is the ability of the same behavior to have multiple different manifestations or forms.
public class StaticSupper {public static String staticGet() {return "Base staticGet()";}public String dynamicGet() {return "Base dynamicGet()";}}class StaticSub extends StaticSupper {pub static String staticGet() {return "Sub staticGet()";}}public String dynamicGet() {return "Sub dynamicGet()";}}class StaticMub extends StaticSupper {pub static String staticGet() {return "Mub staticGet()";}public String dynamicGet() {return "Mub dynamicGet()";}} public class StaticPolymorphism { public static void main (String [] args) { StaticSupper sup1 = new StaticSub(); System.out.println(sup1.staticGet()); System.out.println(sup1.dynamicGet()); StaticSupper sup2 = new StaticMub(); System.out.println(sup2.staticGet()); System.out.println(sup2.dynamicGet()); } }out: Base staticGet() Sub dynamicGet() Base staticGet() Mub dynamicGet()
The concept of program binding:
Binding refers to the association of a method call with the class (the method body) where the method is located. For Java, binding is divided into static binding and dynamic binding; or it is called early binding and late binding.
Static binding:
The method has been bound before the program is executed and is implemented by the compiler or other connecting program. For example: C.
For Java, it can be understood as binding during the program compilation period; here is particularly clear that the only methods in Java are final, static, private and constructor methods that are early binding.
Dynamic binding:
Later binding: Dynamic binding refers to the compiler not knowing which method to call during the compilation stage until the runtime is bound according to the type of the specific object.
If a language implements late binding, it must also provide some mechanisms to determine the type of the object during operation and call appropriate methods separately. In other words, the compiler still does not know the object type at this time, but the method calling mechanism can investigate it by itself and find the correct method body. Different languages have different ways of implementing late binding. But we can at least think this way: they all need to install certain special types of information in the object.
Method overloading includes static method overloading and ordinary method overloading. Static method overloading is static binding, and the method call is through: class name. method. Ordinary method overloading is dynamic binding, and method calls are through: instance object reference. method. The constructor can be overloaded, but cannot be rewritten.
Static methods can be rewritten, but no polymorphic effect is achieved.
Summarize
The above is all about this article's brief discussion on overloading, rewriting, polymorphism, static binding and dynamic binding in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!