1.Overriding
Overriding translation is "rewrite/overwrite". It covers a method and rewrites it to achieve a different function. The most familiar overlay for us is the implementation of the interface method, in which the method is generally declared, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class in the inheritance.
The main features of the rewrite are:
1 The method name must be consistent with the overridden method.
2 The method parameter list must be consistent with the overridden method.
3) The return type must be consistent with the overridden method.
4 The overridden method does not reduce the "visibility" of the original method.
For example, if the overridden method is protected void do (int i,double d), the overriding method can be protected void do (int i,double d), or public void do (int i,double d), but not To be private void do (int i,double d).
5 cannot throw a new exception or a "wider" exception.
For example, if the overridden method is public void do (int i,double d) throws IOException, the overriding method can be either public void do (int i,double d) throws IOException, or PU Blic void Do (int i,double d) throwsddeeexception (IOException subclass), but cannot be a public void do (int i,double d) throwsexception. Because exception is IOException's parent class, it is "wider" than IOException.
6 The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.
2.Overloading
Overloading, translated into "overload". It means that we can define methods with the same name, by defining different input parameters to differentiate these methods, and then calling, the VM chooses the appropriate method to execute according to the different parameter styles.
Its characteristics are:
1 The method name of each overload is consistent.
2 The parameter list of each overloaded method is different (including the parameter type, the number of parameters, one or more items in the parameter order 3 items).
3) The return type is arbitrary. (Overloaded methods cannot be distinguished by the return value of a method.) )
4 The access control character is arbitrary. (The overloaded method cannot be distinguished by the access rights of the method.) )
5 can throw its own exception regardless of the overloaded method. (You cannot differentiate overloaded methods by throwing exceptions.) )
Overloading means "two or more methods can have the same name if they have different numbers or types of parameters and thus di Fferent signatures. "Obviously, the only requirement for overloading is that the parameter list must be changed, otherwise it is not overloaded."
3. Overloads in Type conversions
In some cases, the automatic type conversion of Java also applies to the arguments of overloaded methods. For example, look at the following program:
// Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: " + a);
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)
}
}
The program produces the following output:
No parameters
A and B:10 20Inside test (double) a:88inside test (double) a:123.2
In this case, this version of Overloaddemo does not define test (int). So when you call Test () with an integer argument in overload, you cannot find the method that matches it. However, Java can automatically convert integers to double, which can solve the problem. Therefore, after the test (int) is not found, Java expands I to double, and then calls Test (double). Of course, if test (int) is defined, then of course the call to test (int) does not call Test (double). Java's automatic conversion works only if no exact match is found, and always finds the most "matching" method of the parameter type, that is, its formal parameter is larger than the actual argument and is the most "close".
This article is from the "Zi Postman" blog, please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/20373