Java:Overriding与Overloading

In Java , the What is Overriding? What is Overloading ?
author: ZJ 07-3-13
Blog:  [url]http://zhangjunhd.blog.51cto.com/[/url]
1.Overriding
Overriding translated as "rewrite / cover." It is covered with a method and rewrite them in order to achieve different effects. For us, the most familiar is to achieve coverage of the interface method, the interface in general is just a statement of the method, and when we realize, we need all the methods declared in the interface implementation. In addition to this typical usage, we may also cover the method of the parent class in a subclass in inheritance.
The main features are rewritten:
1) method name must be consistent with the rewriting method.
 
2) method parameter list must be consistent with the rewriting method.
 
3) return type must be overwritten with the same method.
 
4) The method can not be reduced to rewrite "visibility" of the original method.
For example: The method is rewritten as protected void do (int i, double d), the method may override protected void do (int i, double d), or public void do (int i, double d), but not is private void do (int i, double d).
 
5) can not throw new Exception or "broader" exception.
For example: The method is rewritten as public void do (int i, double d) throws IOException, the method may override the public void do (int i, double d) throws IOException, or public void do (int i, double d) throws ddeeException (IOException subclass), but is not public void do (int i, double d) throws Exception, as is the Exception IOException parent class, "wider" than IOException.
 
6) The method can not be covered by the private, or subclasses thereof defines a new method only, and not be overridden.
2.Overloading
Overloading, translated as "overload." It means that we can define some of the same name of the method, by defining the parameters to distinguish between different input methods, and then when, the VM will patterns depending on various parameters, to select the appropriate method for performing call.
Its characteristics are:
1) consistent with the method name of each overloaded.
 
2) the list of parameters is not the same for each overloaded method (including a type of parameters, number of parameters, parameter order three or more).
 
3) any return type. (The return value can not be distinguished by the method of overloaded methods.)
 
4) any access specifier. (Can not access method to distinguish overloaded methods.)
 
5) can throw any exception itself, regardless of the method is overloaded. (Can not throw exceptions to distinguish overloaded methods.)
 
Overloading means "Two or more methods can have the same name if they have different numbers or types of parameters and thus different signatures." Clearly, the only requirement is overloaded argument list must be changed, otherwise it is not overloaded.
3. type conversion overloading
In some cases, Java's automatic type conversion also applies to the argument overloaded methods. For example, consider 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 generates the following output:
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2
 
In the present example, this version does not define OverloadDemo test (int). Therefore, when the integer parameter called tape test () in the Overload, and can not find its matching method. However, Java can be automatically converted to a double integer type, this conversion can solve this problem. Therefore, after test (int) can not be found, Java will i be expanded to double type and then calls test (double). Of course, if the definition of test (int), the first call of course test (int) without calling test (double). Only when no exact match, Java's automatic conversion will work, but the best method parameter type "match" always find that its formal parameter "big" than the argument and is the most "close".
 

Guess you like

Origin www.cnblogs.com/awzh2020/p/12523837.html