Java override (Override) vs. overload (overload) Override (Override)
Overrides are subclasses that rewrite the implementation of the method that allows access to the parent class, and the return value and formal parameters cannot be changed. that is, the shell does not change, the core rewrite!
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.
The overriding method cannot throw a new check exception or a broader exception than the overridden method declaration. For example: A method of the parent class declares a check exception ioexception, but cannot throw Exception exception when overriding this method, because Exception is the parent of IOException, only the subclass exception of IOException can be thrown.
In object-oriented principles, rewriting means that any existing method can be rewritten. Examples are as follows:
Testdog.java File Code:
Class animal{public void Move () { System.out.println ("Animal can be moved");} } class Dog extends animal{public void Move () { System.out.println ("Dog 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 a Method of Nimal class b.move ();//method of executing the Dog class }}
The results of the above example compilation run as follows:
Animals can move dogs can run and walk
As you can see in the example above, although B belongs to the animal type, it runs the Move method of the dog 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.
So in the example above, the reason for compiling successfully is because the Move method exists in the animal class, but at run time, the method of the particular object is running.
Consider the following example:
Testdog.java File Code:
Class animal{public void Move () { System.out.println ("Animal can be moved");} } class Dog extends animal{public void Move () { System.out.println ("Dog 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 = n EW Dog (); The Dog object a.move ();//The method that executes the Animal class B.move ();//The method that executes the dog class B.bark ();}}
The results of the above example compilation run as follows:
Testdog.java:30: Cannot find Symbolsymbol : Method Bark () location:class Animal b.bark (); ^
The program throws a compilation error because the reference type of B animal does not have a bark method.
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 lower 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.
- 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.
Use of the Super keyword
Use the Super keyword when you need to call the overridden method of a parent class in a subclass.
Testdog.java File Code:
Class animal{public void Move () { System.out.println ("Animal can be moved");} } class Dog extends animal{public void Move () { super.move ();//Apply Super class method System.out.println ("Dog can run and walk");}} public class testdog{public static void Main (String args[]) {Animal b = new Dog ();//Dog Object B.move ();//Execute Dog Method of the Class }}
The results of the above example compilation run as follows:
Animals can move dogs can run and walk
Overloading (overload)
Overloading (overloading) is a class in which the method name is the same and 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 overload of the constructor.
Overloading rules
- The overloaded method must change the parameter list (the number of parameters or the type or order is different);
- 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.
- The return value type cannot be used as a distinguishing criterion for overloaded functions.
Instance Overloading.java file code:
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 have different order of 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< c19> 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 |
Overloaded Methods |
overriding Method |
Parameter list |
Must be modified |
Must not be modified |
return type |
can modify |
Must not be modified |
Abnormal |
can modify |
can be reduced or deleted, must not throw new or wider exceptions |
Access |
can modify |
Must not be more restrictive (can reduce the limit) |
Summarize
Overrides of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism, which is a representation of polymorphism between a parent class and a subclass, and overloading can be understood as a concrete representation of polymorphism.
- (1) A method overload is a class in which multiple method names are defined, and the number of their arguments is different or the number is the same and the type and order are different, which is called the overloaded (overloading) of the method.
- (2) A method override is a method in which a subclass exists with the same name as a method of the parent class, and the number of parameters is the same as the type, and the return value is the same as the overriding.
- (3) method overloading is a polymorphic representation of a class, while a method rewrite is a polymorphism representation of a subclass and a parent class.
List of Notes
Overloading is the same method that can be processed differently depending on the input data
Overrides are when the subclass inherits from the same method as the parent class, entering the data, but to make a response different from the parent class, you overwrite the parent class method
-
Overloading is the same method of writing multiple variable modifiers differently, when calling a method, The type of the given number is automatically judged and the method in which it fits is selected.
public class overloading {private static int i = 1 ; private static Double L = 3.1415 ; private static String k = "I am chestnut" ; The parameter type is different public void test (int a) {System.out.println ("you entered an integer:" + a);} public void Test (double a) {System.out.println ("you entered a floating-point number:" + a);} public void Test (string a) {System.out.println ("you entered a string:" + a);} public static void main (string[] args) {overloading Lizi = new overloading (); lizi.test (i); Lizi.test (l); Lizi . Test (k); }}
Output:
you entered an integer: 1 you entered a floating point number: 3.1415 you entered the string: I'm chestnut.
-
Supplement: The parent class declares the variable to point to the subclass instance, and the parent class variable cannot call a variable or method that does not exist in the parent class, otherwise it throws an exception
class animal{public void Move () {System.out.println ("Animals can Move" ); }} class Dog extends animal{public int ; public void Move () {age = ten ; System.out.println ("Dog can run and walk" ), public void bark () {System.out.println ("dog can Bark" )}} public class testoverride{public static void Main (String args[]) {Animal a = new Animal ();//Animal object Animal b = new do g (); The Dog object a.move ();//The method that executes the Animal class B.move ();//The method of executing the Dog class//b.age;//Remove the former comment symbol, will throw exception//B.bark ();//Remove the previous comment symbol, throw an exception }}
A concise understanding of overloading and rewriting:
- overloading reflects the "improvise". The same function, depending on the type of data, takes different approaches. For example, the same is eating, to high-end restaurants to eat Western food, you will wear suits, but to eat hot pot, wearing casual clothes is more appropriate.
- overrides reflect "parent-child differences." You "inherit" the father to eat hot pot hobby, but eat the same pot (note, the same data type), you like to rinse red soup, your father likes to rinse the broth.
About the possibility of changing the parameter list of overloaded functions:
/** * @author Tangjia * @date October 29, 2017 pm 4:29:06 * @version 1.0 * @since JDK 1.8.0_65 * Class Description: Test overloaded function */public class M Ain {public static void main (string[] args) { int = ten; String name = "II"; handle (age); handle (name); Handle (name,age); Handle (Age,name); } public static void Handle (int.) { System.out.println (age); } public static void handle (String name) {System.out.println (name),} public static void handle (int age,string NA Me) {System.out.println ("The Age of" +name+ "was" + Age);} public static void handle (String name, int. ) {System.out.println ("The Age of" +name+ "is" + Age);}}
- 1. Parameter type changes, such as parameter conversion from int to string type
- 2. Change the number of parameters, such as the number of parameters from one to two
- 3. Parameter order changes, such as from handle (int age, string name) to handle (string Name,int age)
- 4. Function return value is immutable
Java override (Override) vs. overload (overload)