Override (Override)
Overrides are subclasses of the implementation of the method that allows access to the parent class to be rewritten!
neither the return value nor the formal parameter can be changed . That is, the shell does not change, rewrite the inner implementation!
The benefit of overriding is that subclasses can define their own behavior as needed .
That is, subclasses can implement methods of the parent class as needed.
In object-oriented principles, rewriting means that any existing method can be rewritten. Examples are as follows:
class Animal{ Public voidMove () {System.out.println ("Animal can Move"); }} class Pig extends Animal{ Public voidMove () {System.out.println ("Pig can Climb tree"); }} Public class Test{ Public Static voidMain (String args[]) {Animal a =NewAnimal (); Animal B =NewPig (); A.move ();//How to execute the Animal classB.move ();//methods for executing Pig classes}}
The results of the above example compilation run as follows:
movePig can Climb tree
As can be seen from the above example, B declares a reference to the animal class, but runs the move method of the pig class.
This is because in the compile phase, only the reference type of the parameter is checked .
At run time, however, the Java Virtual machine (JVM) specifies the type of object and the method that runs the object.
In the example above, it was possible to compile successfully because the Move method exists in the animal class, but at run time, the method of the specific object (Pig object) is running.
Look at the following example:
class Animal{ Public voidMove () {System.out.println ("Animal can Move"); }} class Pig extends Animal{ Public voidMove () {System.out.println ("Pig can Climb tree"); } Public voidBark () {System.out.println ("Pig can Bray"); }} Public class Test{ Public Static voidMain (String args[]) {Animal a =NewAnimal (); Animal B =NewPig (); A.move ();//How to execute the Animal classB.move ();//methods for executing Pig classesB.bark (); }}
The result of compiling the above instance will report the following error:
Test.java:30: cannot find symbolsymbol method bark()location:class Animal b.bark(); ^
This is a compilation error because there is no bark method in the reference type animal class for B.
overriding rules for methods
The argument list must be exactly the same as the overridden method;
The return type must be exactly the same as the return type of the overridden method;
access permissions cannot be higher than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected or private.
A member method of a parent class can only be overridden by its subclasses.
a method that is declared final cannot be overridden .
a method declared as static cannot be overridden, but can be declared again .
Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.
Subclasses and parent classes are not in the same package, the subclass can only override non-final methods that are declared public and protected by the parent class.
The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception.
However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
The construction method cannot be overridden .
If you cannot inherit a method, you cannot override this method (the private method of the parent class).
Overloading (overload)
Overloading (overloading) is the same class in which the method name is the same, and the parameters differ, and the return type can be the same or multiple methods that can be different .
Each overloaded method (only the constructor can be overloaded) must have a unique list of parameter types.
Overloading rules:
The overloaded method must change the parameter list ;
The overloaded method can change the return type;
Overloaded methods 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 .
Public classtest{ Public int Test() {System. out. println ("Overload1");return 1; } Public void Test(intA) {System. out. println ("Overload2"); }//The following two parameter types are in different order PublicStringTest(intA,string s) {return "Overload3"+ S; } PublicStringTest(String S,intA) {return "Overload4"+s; } Public Static void Main(string[] args) {Test T =NewTest (); System. out. println (T.test ()); T.test (1); System. out. println (T.test (1,"Test3")); System. out. println (T.test ("Test4",1)); }}
The difference between overriding and overloading
Overloaded methods:
参数列表 必须修改 返回类型 可以修改 异常 可以修改 访问修饰符 可以修改
Override method:
参数列表 不能改 返回类型 不能改 异常 可以减少或删除,不能抛出新的或更广的异常 访问修饰符 只能降低限制,不能变成高限制
Limit from low to high: public, protected, private
[Java] Usage rules and differences for override and overload