First, there are two meanings of polymorphism in Java, overloading (overload) and overriding (override), which are polymorphism related to inheritance.
The first: The polymorphism of the operation name, that is, there are multiple operations with the same name, but these operation locks must accept different message types.
The second type: polymorphism related to inheritance, which may have different behavior when the same operation is called by different and type objects.
Here is a brief introduction to the first polymorphic, a simple understanding of the overloads of the methods in Java:
So what is overload? That is, a class can have multiple methods with the same name, but the parameters of these methods must be different:
1) The number of parameters is different.
2) The number of arguments is the same, but the type of one parameter in the argument list is different.
It is also important to note that the return type of the method and the name of the parameter do not participate in the comparison, that is, if the names of the two methods are the same, the parameters must be guaranteed to be different, even if the return type is different.
Public class people { public int hello (Int x, int y) { return x+y; } public float hello (float x, int y) { return x+y; } public double hello ( Double x, int y) { return x+y; }}public class testpeople { /** * @author Shepherd's queen, * @param args * @date 2015-10-13 */ public static void main ( String[] args) { People p = new People (); &nbsP; system.out.println (P.hello (10, 10)); //20 system.out.println (P.hello (12.12f, 7)); //19.119999 system.out.println (P.hello (12.03, 7)); //19.03 }}
5. In the case of overloading of methods, it is also necessary to avoid the ambiguity of overloading, that is, it is unclear which of the overloaded methods should be executed, so that the program cannot compile.
void cry (double x, int y) {...} void Cry (int x,double y) {...} p.cry (10,10); A prompt message appears: The reference to cry is ambiguous.
Method Overloading (overload)