Java overloading and overwriting, and java Overwriting

Source: Internet
Author: User

Java overloading and overwriting, and java Overwriting

Many students are not clear about overload and override. We recommend that you do not memorize conceptual knowledge and remember it.

First, let's give my definition:

Overload: A method group with the same name and different parameters in the same or inherited class. The essence is the name of different methods.

Override (overwrite): between two classes with an inheritance relationship, the methods existing in the parent class are redefined in the subclass. Essentially, different implementations are provided for the same method.

 

Let's take a look at the example of heavy load:

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 heavy load method ?");

}

}

 

Public class OverloadChildextends OverloadParent {

Public void getPerson (double money ){

Sytem. out. println ("Am I a heavy load method ");

}

}

Note:

(1) OverloadParent has four methods with the same name.

(2) The parameter types and numbers of the first three methods are inconsistent, and the return values are consistent, which constitutes a heavy load

(3) Method 4 and method 1 only return different values. They do not constitute heavy loads and the compiler does not pass.

Ps: the return value is the result of method execution. When calling a method, we do not specify "I want to call a method with a return value of the xxx type". This is not a feature of method overloading.

(4) OverloadParent inherits the Demo and all methods in the Demo. It felt that the existing method could not meet the requirements and simply overloaded one.

 

Overload flag: The method name is the same, and the parameters are different (number or type), which is irrelevant to the return value.

 

Let's 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 the main method of OverrideChild is executed?

The answer is: I can't fly, but I can run!

We can see:

(1) Both OverrideChild and OverrideParent have a fly method.

(2) the return value and modifier of fly are the same, and only the method bodies are different.

(3) There is a @ overwrite annotation 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 is helpful for Compiler checks.

Mark of overwriting: The child inherits the parent class and has different implementations for the same method.

 

Application scenarios

Overload: When methods have similar functions, different parameters must be passed.

Override: a subclass has its own unique behavior. It inherits from the parent class and cannot meet its own needs.

Ps: both heavy load and overwrite are the manifestations of polymorphism. The former is compiler polymorphism, and the latter is Runtime polymorphism.

 

Follow ginger to talk about technology, no.: helojava, or scan the following QR code.


One post per day, chicken soup for technical purposes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.