Learning Diary (ix) Java method overloading and rewriting

Source: Internet
Author: User

Overloading of methods: two or more than two methods in a class whose method name is the same as the parameter list, which is called the overloading of the method, and the overloading of the method is one of the ways of implementing polymorphism.

Parameter lists differ by the number of parameters or the same number of arguments, but in different order or type, the return value type can be different when the method overloads, but only if the return value type is different.

When Java calls the method, if the exact match does not have a corresponding method, it will automatically convert to match the corresponding method, for example, if there is no parameter of type int, but there is a method of the same name with a parameter of type double, if the parameter of int type is passed when the method is called, it will not error. Instead, an int is automatically converted to a double type that matches a double type parameter. However, this situation only occurs if the exact match does not match, and if this method exists with the int type parameter, the type argument is not invoked when the double type is passed.

The value of overloading is that it allows related methods to be accessed with the same name, and there is no rule that requires a relationship between overloaded methods, but in fact, if there is no relationship between the methods should not be implemented with overloading, but should use a new method, otherwise it would violate the original intention of overloading.

Common methods can be overloaded, and construction methods can be overloaded as well. Whether it is a normal method or a construction method, the parameters passed inside are either simple or object. A simple type directly transmits a value, whereas an object passes a reference.

Overloading follows a "compile-time binding," which determines which method is called at compile time based on the parameters.

Method overrides: When the subclass inherits the parent class, the subclass is exactly the same as the parent class method name and argument list, but other places are different, which is called rewriting. Overrides occur only at the time of inheritance, and cannot be overridden if a class cannot be inherited. The override of a method is also known as the override of a method.

The overriding principle is that the method name is the same, the parameter list is the same, the subclass throws an exception that is less than or equal to the exception thrown by the parent class, and cannot throw an exception that the parent class does not have, and the return value type of the subclass is less than or equal to the return value type of the parent class;

In addition to the above-mentioned principles, it is also known that the method that is identified as final cannot be overridden and cannot be overridden by methods that cannot be inherited, such as the private adornment method.

A method overridden by a quilt class object that determines which version of a method to call, depending on the type of the object (rather than the reference type) when the program is run, the reference type before the equals sign, and the object type after new.

Overrides follow run-time binding, which is the method version that is invoked at run time, depending on the actual object type that the reference points to.


Learning Diary (ix) Java method overloading and rewriting

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.