Method overloading and overrides in Java (overwrite)

Source: Internet
Author: User

I. Method Overloading (OVERLOAD)

/* Method overload judgment: In the same class, the method name is the same, the parameter list is different (the number of parameters is different, the parameter list corresponds to the position parameter type), the other method return value * and access modifiers are arbitrary. */public class Overloadtest {public void testFunc (int a, int b, float c) {}//The first situation:the number of Param tabl E is different.public void TestFunc (int a, int. b) {}//The second situation:the type of param table is different.//parameter type The meaning is that: The parameter list corresponds to the type of position, and the parameter name is not any connection, so in the process of judging the method overload//regardless of the parameter order changes. public void TestFunc (int A, float c, int b) {}/* * the following function isn ' t overload *///error Message: Duplicate method Testfun c (int, int, float) in type overloadtest//with the first method repeated the error, because the parameters A and B are the same type, regardless of whether the parameter order is changed, and finally in the process of the method record,//or will be recorded as the above error message prompts, This parameter list is identical to the argument list of the first function. public void TestFunc (int b, int A, float c) {}//error message: Duplicate method TestFunc (int, float, int) in type overloadtest//although the The function also exchanges the parameter order, but the exchange of the two parameter types are different, resulting in a parameter list corresponding to the method one location type,//and method one did not cause duplicate error, but with method three has caused repeated error, should be for their parameter list is (int, float, int). It can also be seen from here that the parameter names are not considered in the decision of the method overload. public void TestFunc (int A, float c, int b) {}}


Two. Method overrides (Overwrite)

Method overrides are also called method overrides, which indicate that subclasses want to modify a method of a parent class, and the method overrides are simple and generally follow the following guidelines:

1. Two same as: Method name and method parameter list

2. One large: Overridden method in a subclass has greater access than equals to a method in the parent class

3. Two small: An overriding method in a subclass throws an exception type that is less than or equal to the parent class; The return value type of an overriding method in a subclass is less than or equal to the parent class


Method overloading and overrides in Java (overwrite)

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.