Many students for overload and override silly points not clear, it is recommended not to memorize the concept of knowledge, to understand the Memory.
First give me the definition:
Overload (overloaded): A group of methods with the same name and different parameters in the same class or class that has an inheritance Relationship. The essence is the name of the different methods.
Override (overwrite): a method that exists in the parent class is redefined in the subclass between the two classes that have inheritance Relationships. The essence is to give different implementations for the same method.
Let's look at an example of overloading first:
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 a method of Overloading?") ”);
}
}
public class Overloadchildextends Overloadparent {
public void Getperson (double Money) {
Sytem.out.println ("am I a method of overloading");
}
}
Watch Out:
(1) in overloadparent, There are 4 methods with the same name
(2) the parameter types and number of the first three methods are inconsistent, and the return values are identical, constituting the overloaded
(3) Method 4 and Method 1 only return value is not the same, does not constitute an overload, the compiler does not pass.
Ps: The return value is the result of the method execution, we do not specify when calling the method "i want to call the return value is XXX type method", It is not a feature of the method Overload.
(4) Overloadparent inherited the methods owned by demo,demo, which all Possessed. It felt that the existing method could not meet the demand, simply overloaded One.
Overloaded Flags: method names are the same, parameters differ (number or type), regardless of the return Value.
Let's look at an example of the overwrite:
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 is the output of the main method of executing overridechild?
The answer is: I can ' tfly, but I can run!
We see:
(1) Overridechild and Overrideparent have a fly method
(2) Fly return value and modifier are the same, only the method body is not the same
(3) there is a @overwrite annotation before the fly method of the subclass, jdk1.5 appears, only for class inheritance, 1.6 can be used for interface Implementation. This annotation helps the compiler to check and not add.
Overwrite Flag: sub-parent class, The same method has a different Implementation.
Application Scenarios
Overloading: when the method functions similarly, it needs to pass different Parameters.
Overwrite: a subclass has its own unique behavior, inherited from the parent class, and does not meet its own needs.
Ps: overloading and overwrite are polymorphic manifestations. The former is a compiler polymorphism, and the latter is a run-time Multi-state.
Attention to old Ginger talk about technology, number: helojava, or scan the following QR Code.
One post per day, technical chicken SOUP.
Java Overloading and overwrite