The first to see these two words and no special feeling, but the time is long, but found that the book for a while with the override, and a while with overload, make me confused. So I made a summary, I hope to be the same with me on these two vague concepts of the netizen has a help.
Override can be translated as an overlay, literally knowing that it is covering a method and rewriting it to achieve different effects. The most familiar overlay for us is the implementation of the interface method, which is generally just a declaration of the method in the interface, 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 with the subclass in the inheritance. Be aware of the following points in the overlay:
1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;
2. The return value of the overridden method must be the same as the return of the overridden method;
3. The exception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4, the overridden method cannot be private, otherwise only a new method is defined in its subclass and is not overwritten.
Overload may be more familiar to us, can be translated as overloaded, it means that we can define some of the same name, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, to choose the appropriate method of execution. The following points are to be noted in using overloading:
1.you can only pass different parameter styles when using Overloads。 For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must differ, such as can be fun (int, float), but not fun (int, int));
2, cannot be overloaded with access rights, return types, thrown exceptions;
3.the exception type and number of methods do not affect overloading;
4, for inheritance, if a method is priavte in the parent class, it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.
The following is a test program for override and overload, where the contents of the comments are code that generates a compilation error, and we remove the comments to see what happens at compile time.
Files tested on Overload: Overloadtest.java
public class Overloadtest {
The following methods are used to verify that method overloading can be done by defining different parameter types and the number of parameters.
public void Fun () {
System.out.println ("Method fun in Overloadtest, no parameter");
}
public void fun (float f) {
System.out.println ("Method fun in Overloadtest, parameter Type:float");
}
public void Fun (int i) {
System.out.println ("Method fun in Overloadtest, parameter Type:int");
}
public void Fun (int i1, int i2) {
System.out.println ("Method fun in Overloadtest, parameter type:int, int");
}
The following two methods are used to verify that method overloads can be made by defining different parameter sequences.
Note: The parameters here are definitely not the same type, otherwise the order of the sequence will be meaningless.
public void fun1 (int i, float f) {
System.out.println ("Method fun1 in Overloadtest, sequence of parameters is:int, float");
}
public void fun1 (float f, int i) {
System.out.println ("Method fun1 in Overloadtest, sequence of parameters is:float, int");
}
The following two methods are used to verify the effect of the exception thrown by the method on overloading.
Neither the type of the exception nor the number of exceptions will have any effect on overloading.
public void Fun2 () throws TestException {
System.out.println ("Fun2 in Overloadtest, exception:testexception");
}
public void fun2 (int i) throws TestException, TestException1 {
System.out.println ("Fun2 in Overloadtest, Exception:testexception, TestException1");
}
public void fun2 (float f) throws Exception {
System.out.println ("Fun2 in Overloadtest, exception:exception");
}
You cannot overload the fun method by throwing an exception type.
public void Fun (int i) throws Exception {
System.out.println ("Method fun in Overloadtest, parameter type:int, exception:exception");
//}
You cannot overload the fun method with a return value.
public boolean fun (int i) throws Exception {
System.out.println ("Method fun in Overloadtest, parameter Type:int, exception:exception, Return:boolean");
return true;
//}
private void Fun3 () {}
cannot be overloaded with different access rights
public void Fun3 () {}
public static void Main (string[] args) {
This only defines an instance of Overloadtest, so test does not call
The method in OverloadTest1.
Overloadtest test = new OverloadTest1 ();
This defines an instance of OverloadTest1, because OverloadTest1 is overloadtest
, so test1 calls the method in Overloadtest.
OverloadTest1 test1 = new OverloadTest1 ();
try {
int i = 1, j = 2, M = 3;
There's no way to call OverloadTest1 's Fun method.
Test.fun (I, M, j);
Test1.fun (i, J, M);
Test1.fun ();
This call will not be executed because FUN3 () access rights in Overloadtest are PRIAVTE
Test1.fun3 ();
TEST1.FUN3 (i);
} catch (Exception e) {}
}
}
Class OverloadTest1 extends overloadtest{
Reload Fun in subclasses
public void Fun (int i, int m, int n) {
System.out.println ("Overload fun1 in OverloadTest1, parameter type:int, int, int");
}
This is not an overload of the method in the parent class, it's just a new method.
public void fun3 (int i) {
System.out.println ("fun2 in OverloadTest1");
}
}
Files for the override test: Overridetest.java
public class Overridetest {
public void Fun () throws TestException {
System.out.println ("Method fun in Overridetest");
}
private void Fun1 () {
System.out.println ("Method fun1 in Overridetest");
}
public static void Main (string[] args) {
Overridetest test = new OverrideTest1 ();
try {
Test.fun ();
Test.fun1 ();
} catch (Exception e) {}
}
}
Class OverrideTest1 extends overridetest{
The following normal override
public void Fun () throws TestException2 {
System.out.println ("Fun in OverrideTest1");
}
You cannot override a method in a parent class because it defines different exception types and
The return value.
public int Fun () throws TestException1 {
System.out.println ("Method fun in Test");
return 1;
//}
Cannot override a method in the parent class because it throws an illegal range than the parent class
A larger exception.
public void Fun () throws Exception {
System.out.println ("Fun in OverrideTest1");
//}
This method does not override the Fun1 method in the parent class, because this method
The parent class is a private type, so this is just the equivalent of defining a new method.
public void Fun1 () {
System.out.println ("Method fun1 in Test");
}
}
Class TestException extends exception{
Public testexception (String msg) {
Super (MSG);
}
}
Class TestException1 extends TestException {
Public TestException1 (String msg) {
Super (MSG);
}
}
Class TestException2 extends TestException {
Public TestException2 (String msg) {
Super (MSG);
}
}
The difference between overloading and overwriting (also called rewriting) in Java