There are two types of polymorphism in Java: overload and override. Method overloading is one of the two types of polymorphism.
For example: when you ask a person to perform the "find area" operation, he may ask you what area you are looking for?
Functional polymorphism means that different messages can be passed to functions so that the object can produce corresponding behaviors based on the corresponding messages. The behavior of an object is reflected through the methods in the class, so the polymorphism of behavior is the overloading of methods.
Syntax rules for method overloading:
Method overloading means that there can be multiple methods with the same name in a class, but the parameters of these methods must be different. Either the number of parameters is different, or the number of parameters is the same, but the type of a corresponding parameter in the parameter list is different.
For example:
classPeople{floathello(inta,intb){returna+b;}floathello(longa,intb){returna-b;}doublehello(doublea,intb){returna*b;}}publicclassMain{publicstaticvoidmain(Stringargs[]){Peopletom= newPeople();System.out.println(tom.hello(10,10));System.out.println(tom.hello(10L,10));System.out.println(tom.hello(10.0,10) );}}
The running results are as follows:
20.00.0100.0
Notice:
If two methods have the same name, the parameters must be different even if the return types are different.