The difference between rewriting, overloading, and overwriting in java, and overwrite in java

Source: Internet
Author: User

The difference between rewriting, overloading, and overwriting in java, and overwrite in java
Polymorphism
Through inheritance, a class can be used as multiple types: it can be used as its own type, any base type, or any interface type when implementing an interface. This is called polymorphism.

Heavy Load
Each type of member has a unique signature. A Method signature consists of a method name and a list of parameters (the order and type of parameters. As long as the signature is different, you can define multiple methods with the same name in one type. When two or more methods with the same name are defined, they are called overload. That is, the parameter lists of members with the same name are different (Parameter order and type) during reload ).

Inherit, override, and hide members (override = overwrite)
The derived type inherits all the members of its basic type. That is, these members are defined on the derived type and can be used for the derived type. The behavior and quality of inherited members can be modified in the following two ways:

1. You can use the same signature to define a new member for the derived type to hide the inherited member. This method can be used to convert a previous public member into a private member, or to define a new line for an inherited method marked as final.

2. You can override the inherited virtual method of the derived type. The override method provides a new definition of the method, which calls the method based on the type of the value at run time, rather than the variable type known at compile time. Only when the virtual method is not marked as final and the new method can at least be accessed like the virtual method can the Member rewrite the virtual method.
Method names and parameters are the same to form rewriting. The method to be rewritten cannot reduce the "visibility" of the original method, nor change the type of return value of the original method.

Methods With the same name and different parameters (numbers and types) form heavy loads. The overloaded methods can be regarded as a brand new method, compared with the original method, it can have different "visibility" and "Return Value Type ". For example:

Class {
Protected int method1 (int a, int B) {return 0 ;}
}
Public class B extends {
Public int method1 (int a, int B) {return 0;} // correct. Override the parent class method to extend the access permission.
// Private int method1 (int a, int B) {return 0;} // error. Rewrite the parent class method and the access permission cannot be reduced.
// Private long method1 (int a, int B) {return 0;} // error. Rewrite the parent class method and do not change the type of the returned value.
Public short method1 (int a, long B) {return 0;} // correct, reload its own method, you can have different access permissions and return value types
Private int method1 (int a, long B) {return 0;} // correct, reload its own method, you can have different access permissions and return value types
}

However, the public short method1 (int a, long B) {return 0;} method and the private int method1 (int a, long B) {return 0;} method cannot both exist, in the same category, methods with the same name and parameter type (override methods) are not allowed ).
Java overloading and rewriting are different.

Java rewriting refers to re-defining a method of the parent class in the subclass. The method name and parameter location and number of the subclass are the same as those of the parent class, therefore, the method of the parent class is not executed when this method of the subclass is called. if methods defined in final are used in the parent class, they cannot be overwritten in the subclass.

Differences between reload and rewriting in java

It should be overload and overwrite; overload is to define multiple methods with the same name and different forms in the same class; overwrite is to overwrite the parent class method for the subclass, the method defined by the parent class is redefined in the subclass, and the same form method from the parent class is hidden in the subclass. The super keyword can be used to access the parent class method covered by the quilt class. The same form means that the method name is the same, and the parameter list is the same (including the type, order, and number of parameters); otherwise, the form is different.

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.