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:
overload: In the same class or in the inheritance relationship, a method group with the same name and different parameters. Essentially, it is the name of different methods.
override: Between two classes that have inheritance relationships, the methods that exist in the parent class are redefined in the subclass. The essence is to give different implementations for the same method.
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("Am 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.
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.
Thank you for reading, I hope it can help you. Thank you for your support for this site!