Frequently Asked Questions about rewriting and overloading

Source: Internet
Author: User

 

Generally, a derived class inherits the methods of the base class. Therefore, when an Object Inheritance method is called, the implementation of the base class is called and executed. however, it is sometimes necessary to implement different inheritance methods in the derived class. for example, assume that the animal class has a "run" method, from which the horse and the dog are derived. The running forms of the horse and the dog are different. Therefore, the same method requires two different implementations, this requires "re-writing" methods in the base class.

Rewrite can only be used between parent class and subclass!

Rewrite Rules: the method in the subclass must be the same as the method in the parent class, and the range can only be expanded but cannot be reduced, for example, if the method defined in the parent class is a subclass of the Public type, it cannot be written as the protected type. Otherwise, a compilation error will be reported! The subclass defined as protected in the parent class can be defined as public


In a class definition, you can write several methods with the same name, but as long as their signature parameter list is different, Java regards them as the only method. Simply put, a method in a class has the same name as another method, but the parameter table is different. This method is called an overloaded method.

Overload is a method that allows functions and operators to process different types of data or receive different numbers of parameters.

 
Reload rules: Reload occurs only in the same type, and is called an overload only when the parameter list is different (including the order, type, and number of parameter lists, A list of parameters with different return value types cannot be called a overload! And the type of the returned value of the overload can be different.

 
The following are frequently asked questions about overloading and rewriting:

 

Package test; public class overloaddemo {/*** @ Param ARGs */public static void main (string [] ARGs) {overloaddemo o = new overloaddemo (); O. method (1, 's'); // there is no method consistent with the parameter, in this case, there will be a nearby principle for implicit conversion, such as converting Char to int is more convenient, so woainitingzai} public int method (int A, long B) {system. out. println ("helloword"); Return 0;} public long method (int A, short B) {// reload is considered to be a heavy load of system only by checking whether the order, type, and number of parameter lists are different. out. println ("Tingting ");/ /If the return value type is different, the return 0;} public int method (int A, int B) {system. out. println ("woainitingzai"); Return 0;} // public short method (int A, int B) {not an overloaded compilation error // system. out. println ("I love you, Ting! "); // Return 0; //} class t extends overloaddemo {// public long method (int A, long B) {compilation error: If the parameter list of the subclass is the same as that of the parent class and the return value type is different, a compilation error is reported. // system. out. println ("hello"); // return 0; //} public int method (int A, char C) {// if there is no compilation error, it is neither overwrite nor overwrite, that is, the return value type must be the same as the method of the parent class. Return 0;} // Private long method (int, short B) {// compilation error. If you change the method of the parent class to private, no compilation error will occur. // return 0; //} // long method (int, short B) {// narrowing the scope of the parent class compilation error // return 0; //} // protected int method (int A, long B) {// narrowing the scope of the parent class compilation error // return 0 ;////}}

 


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.