Java Override and Overload
Override
rewrite vb.net tutorial Is the of a child class to a parent class c# tutorial allow access to python basic tutorial Square of java basic course Legal sql tutorial The implementation process is rewritten, and the return value and formal parameters cannot be changed. That is, the shell remains unchanged and the core is rewritten!
The advantage of rewriting is that subclasses can define their own specific behavior as needed. That is, the subclass can implement the methods of the parent class as needed.
Overridden methods cannot throw new check exceptions or exceptions that are broader than the declaration of the overridden method. For example, a method of the parent class declares a check Exception IOException, but you cannot throw an Exception when overriding this method, because Exception is the parent class of IOException and can only throw exceptions of subclasses of IOException.
In the object-oriented principle, rewriting means that any existing method can be rewritten. Examples are as follows:
class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ System.out.println("Dogs can run and walk"); } } public class TestDog{ public static void main(String args[]){ Animal a = new Animal(); // Animal object Animal b = new Dog(); // Dog object a.move();// Execute the methods of the Animal class b.move();//Execute the method of the Dog class } }
The operation results are as follows:
Animals can move Dogs can run and walk
In the above example, we can see that although b is of type Animal, it runs the move method of Dog class.
This is because at compile time, only the reference type of the parameter is checked.
However, at run time, the Java virtual machine (JVM) specifies the type of object and runs the method of the object.
Therefore, in the above example, the reason why the compilation is successful is that there is a move method in the Animal class. However, at runtime, the method of a specific object is running.
Let's look at the following examples:
class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ System.out.println("Dogs can run and walk"); } public void bark(){ System.out.println("Dogs can bark"); } } public class TestDog{ public static void main(String args[]){ Animal a = new Animal(); // Animal object Animal b = new Dog(); // Dog object a.move();// Execute the methods of the Animal class b.move();// Execute the method of the Dog class b.bark();// Compilation error. The error reason is that the reference object of type b does not have a bark method } }
Overridden rules
- The parameter list must be exactly the same as the parameter list of the overridden method.
- The return type can be different from that of the overridden method, but it must be a derived class of the return value of the parent class (the return type of java5 and earlier versions should be the same, and java7 and later versions can be different).
- The access permission cannot be lower than the access permission of the overridden method in the parent class. For example, if a method of the parent class is declared public, overriding the method in the child class cannot be declared protected.
- Member methods of a parent class can only be overridden by its subclasses.
- Methods declared final cannot be overridden.
- Methods declared as static cannot be overridden, but can be declared again.
- If the subclass and the parent class are in the same package, the subclass can override all methods of the parent class, except those declared as private and final.
- If the subclass and the parent class are not in the same package, the subclass can only override the non final methods declared as public and protected by the parent class.
- The overridden method can throw any non mandatory exception, whether or not the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a wider range of mandatory exceptions than those declared by the overridden method, and vice versa.
- Constructor cannot be overridden.
- If you cannot inherit a class, you cannot override the methods of that class.
Use of super keyword
When you need to call a rewrite method of a parent class in a subclass, use the super keyword.
class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ super.move(); // Method of applying super class System.out.println("Dogs can run and walk"); } } public class TestDog{ public static void main(String args[]){ Animal b = new Dog(); // Dog object b.move(); //Execute the method of the Dog class } }
Animals can move Dogs can run and walk
@Function of Override
@Override is pseudo code, which means rewriting. (of course, it's OK not to write @ override), but it has the following advantages:
1. Can be used as notes for easy reading;
2. The compiler can verify whether the method name under @ Override is all in your parent class. If not, an error will be reported. For example, if you don't write @ Override and you write the following method name wrong, your compiler can compile it because the compiler thinks this method is an added method in your subclass.
For example, when overriding onCreate of the parent class, adding @ Override to the method can help you check the correctness of the method.
@Override public void onCreate(Bundle savedInstanceState) {......}
This is correct if you write:
@Override public void oncreate(Bundle savedInstanceState) {......}
The compiler will report the following error: The method oncreate(Bundle) of type HelloWorld must override or implement a supertype method to ensure that you rewrite the oncreate method correctly (because oncreate should be oncreate).
If you do not add @ Override, the compiler will not detect errors, but will think that you have defined a new method for subclass: oncreate
Overload
Overloading is in a class. The method names are the same, but the parameters are different. The return type can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
The most common place is the overloading of constructors.
Overload rules:
- The overloaded method must change the parameter list (the number or type of parameters are different);
- Overloaded methods can change the return type;
- The overloaded method can change the access modifier;
- Overloaded methods can declare new or broader check exceptions;
- Methods can be overloaded in the same class or in a subclass.
- The return value type cannot be used as a distinguishing criterion for overloaded functions.
public class Overloading { public int test(){ System.out.println("test1"); return 1; } public void test(int a){ System.out.println("test2"); } //The following two parameter types are in different order public String test(int a,String s){ System.out.println("test3"); return "returntest3"; } public String test(String s,int a){ System.out.println("test4"); return "returntest4"; } public static void main(String[] args){ Overloading o = new Overloading(); System.out.println(o.test()); o.test(1); System.out.println(o.test(1,"test3")); System.out.println(o.test("test4",1)); } }
The difference between overriding and overloading
Distinguishing points | Overloading methods | override method |
---|---|---|
parameter list | Must be modified | It must not be modified |
Return type | Can be modified | Can be modified (derived from parent class) |
abnormal | Can be modified | It can be reduced or deleted, and new or broader exceptions must not be thrown |
visit | Can be modified | There must be no stricter restrictions (restrictions can be reduced) |
summary
Method overriding and overloading are different manifestations of java polymorphism. Rewriting is a manifestation of polymorphism between parent and child classes. Overloading can be understood as a specific manifestation of polymorphism.
- (1) Method overloading is defined in a class with multiple methods with the same name, but the number of their parameters is different or the number is the same, but the type and order are different. It is called method overloading.
- (2) Method overriding is a method that exists in a subclass, has the same name as the method of the parent class, has the same number of parameters and types, and has the same return value. It is called overriding.
- (3) Method overloading is a polymorphic representation of a class, while method rewriting is a polymorphic representation of subclasses and parent classes.