Differences between Java rewriting and overloading and precautions

Source: Internet
Author: User

Question No: 3
1. Class {
2. Protected int Method1 (int A, int B) {return 0 ;}
3 .}
Which two are valid in a class that extends Class? (Choose two)
A. Public int Method1 (int A, int B) {return 0 ;}
B. Private int Method1 (int A, int B) {return 0 ;}
C. Private int Method1 (int A, long B) {return 0 ;}
D. Public short Method1 (int A, int B) {return 0 ;}
E. Static protected int Method1 (int A, int B) {return 0 ;}

The standard answer for the question in 310-035 is a, c

 

A is not an overload, and A is a rewrite! That is, overwrite!
Overwrite cannot reduce the "visibility" of the original method"

Class {
Protected int Method1 (int A, int B) {return 0 ;}
}

Class B extends {
Public int Method1 (int A, int B) {return 0 ;}
}

Then there is only one Method1 (INT, INT) method in B!

--------------------------------------------
Reload is:
When multiple methods have the same name and contain different parameters
Different methods are called for different parameters.
It can also be understood that there are actually two methods with the same name and different parameters!

Class {
Protected int Method1 (int A, int B) {return 0 ;}
}

Class B extends {
Private int Method1 (int A, long B) {return 0 ;}
}
B has two methods: Method1 (INT, INT) and Method1 (INT, long!

Because "yes" is two methods
Class B extends {
Public int Method1 (int A, long B) {return 0 ;}
}
It is also correct

-------------------------------------------------------------------------

-------------

Note:

Overwrite (do not know if it is overwrite) Note
The "visibility" of the original method cannot be reduced"

Heavy Load (not just overload)
Overload can be formed only when parameters are different. "visibility", and different return types cannot constitute overloading of methods.

Overload is not:
Re-write (load)

Reload is:
Multiple (possible) Loads

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.