Java-based differences between overload, overwrite, and hide

Source: Internet
Author: User

Overload: multiple functions with the same name but different parameters

Note: 1. Different parameters mean that the parameter type, number of parameters, and order must be at least different.

2. return values, exceptions, and access modifiers cannot be used as overload conditions (for anonymous calls, there will be ambiguity, such as: void a () and int (), if a () is called, ambiguity occurs)

3. The main method can also be overloaded.

Override: The subclass overrides the parent class method. The method name and parameter type must be exactly the same (the parameter cannot be a subclass). The return value and exception are smaller or the same as the parent class (that is, the parent class subclass ), the access modifier is larger or the same than the parent class.

Two, two, two, small, and big

Note: The subclass instance method cannot overwrite the static method of the parent class. The static method of the subclass cannot overwrite the instance method of the parent class (an error is reported during compilation). In summary, the method cannot overwrite each other.

Hide: when the parent class and the subclass have attributes or methods with the same name, the attributes or methods of the parent class with the same name disappear. Actually, they still exist.

Note: When a class is declared as hidden, the corresponding class attributes or methods are called without dynamic binding.

There is only one form of method hiding, that is, the parent class and the subclass have the same static method.

Properties can only be hidden and cannot be overwritten.

Subclass instance variables/static variables can hide the instance/static variables of the parent class. In summary, variables can be cross-hidden.

Differences between hiding and overwriting:

Hidden attribute. After the subclass is forcibly converted to the parent class, the attribute in the parent class is accessed.

The method to be overwritten. After the subclass is forcibly converted to the parent class, the method of the subclass itself is called.

Because overwriting is dynamic binding and is subject to RTTI (run time type identification, runtime type check) constraints, hiding is not subject to RTTI constraints. In summary, RTTI only applies to overwriting, not hiding

Special cases:

1. final-modified attributes can be hidden, but cannot be assigned values. That is, they cannot be assigned values by =. Online, final attributes cannot be modified, which is inaccurate, after a variable of the reference type is modified with final, it cannot be directed to other objects, but its own value can be changed. You can use ArrayList to test the variable, the final attribute can be initialized during running, but the initialization statement does not appear.

2. The final modification method cannot be overwritten and can be overloaded.

3. The final modified class cannot be inherited.

4. The private method implicitly adds final.

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.