Many students are stupid and can't tell the difference between overload and override. It is recommended not to memorize conceptual knowledge by rote, but to understand and memorize it.
Let me first give my definition:
First, let’s talk about: Overloading
(1) Method overloading is a means to let classes process different types of data in a unified way. Multiple functions with the same name exist at the same time, with different parameters/types. Overloading is a manifestation of polymorphism in a class.
(2) Java method overloading means that multiple methods can be created in a class, they have the same name, but have different parameters and different definitions. When calling methods, we decide which method to use by the number of different parameters and parameter types passed to them. This is polymorphism.
(3) When overloading, the method name must be the same, but the parameter type and number are different, and the return value type can be the same or different. The return type cannot be used as the distinction criteria for overloading functions.
Let's first look at the example of overloading:
public class OverloadParent{ public String getPerson(String name){ return "personA" + name; } public String getPerson(int age){ return "personB"; } public String getPerson(String name,int age){ return "personC"; } public void getPerson(String name){ System.out.println("Am I overloaded method?"); }}public class OverloadChildextends OverloadParent { public void getPerson(double money){ System.out.println("Is I an overloaded method"); }}Pay attention to observation:
(1) There are 4 methods with the same name in OverloadParent
(2) The parameter types and numbers of the first three methods are inconsistent, and the return value is the same, which constitutes an overload
(3) Method 4 and Method 1 only return values are different, and do not constitute overloading, and the compiler does not pass.
ps: The return value is the result of the method execution. When we call the method, we will not specify "I want to call a method with the return value of xxx type", it will not become a feature of method overloading.
(4) OverloadParent inherits Demo, and it has all the methods owned in Demo. It felt that the existing method could not meet the needs, so it simply overloaded one.
Overloaded flags: The method name is the same, the parameters are different (number or type), and have nothing to do with the return value.
Then let's talk about overriding (Overriding)
(1) The polymorphism between the parent class and the child class redefines the functions of the parent class. If a method defined in a subclass has the same name and parameters as its parent class, we say that the method is overriding. In Java, subclasses can inherit methods from the parent class without rewriting the same method.
But sometimes the subclass does not want to inherit the parent class's methods intact, but rather wants to make certain modifications, which requires rewriting of the method.
Method rewriting is also called method overwriting.
(2) If the method in the subclass has the same method name, return type and parameter table as a method in the parent class, the new method will override the original method.
If you need the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.
(3) The access modification permissions of subclass functions cannot be less than those of the parent class;
Let’s take a look at an example of overwriting:
public class OverrideParent{ public void fly(){ System.out.println("Ican fly!"); } } public class OverrideChild extends OverrideParent{ @override public void fly(){ System.out.println("Ican't fly, but I can run!"); } public static vid main(String[] args){ OverwriteParent child= new OverwriteChild(); child.fly(); } }What will be output when executing the main method of OverrideChild?
The answer is: I can'tfly, but I can run!
We see:
(1) OverrideChild and OverrideParent have a fly method
(2) The return value and modifier of fly are the same, only the method body is different
(3) There is an annotation of @overwrite before the fly method of the subclass. jdk1.5 appears and is only used for class inheritance. 1.6 can be used for interface implementation. This annotation helps compiler check, and it is OK to not add it.
Overridden flags : The child inherits the parent class and has different implementations for the same method.
Application scenarios
Overload: When the method functions are similar, but different parameters need to be passed.
Override: When a subclass has its own unique behavior, it inherits from the parent class and cannot meet its own needs.
ps: Overloading and overwriting are both manifestations of polymorphism. The former is compiler polymorphism, while the latter is runtime polymorphism.
Rules for rewriting methods:
1. The parameter list must be exactly the same as the method being rewritten, otherwise it cannot be called rewrite but overloaded.
2. The returned type must always be the same as the return type of the overwritten method, otherwise it cannot be called rewrite but overloaded.
3. The access modifier limit must be greater than the access modifier of the rewritten method (public>protected>default>private)
4. The rewrite method must not throw new check exceptions or more broadly checked exceptions than the rewrite method declaration. For example:
A method of the parent class declares an IOException checking exception. When rewriting this method, you cannot throw Exception, you can only throw a subclass exception of IOException, and you can throw non-checking exceptions.
And the rules for overloading:
1. Must have different parameter lists;
2. There can be a return type that does not scold you, as long as the parameter list is different;
3. There may be different access modifiers;
4. Different exceptions can be thrown;
The difference between rewriting and overloading is:
Rewriting polymorphism works, calling overloaded methods can greatly reduce the input amount of code. As long as the same method name passes different parameters into it, it can have different functions or return values.
If you use rewriting and overloading well, you can design a class with clear and concise structure. It can be said that the role of rewriting and overloading in the process of writing code is extraordinary.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.