The difference between overloading, overwriting, and hiding in Java

Source: Internet
Author: User

Overloaded: Multiple functions with the same name but different parameters

Note: 1. Different parameters mean parameter type, number of parameters, parameter order at least one different

2. Return values and exceptions, as well as access modifiers, cannot be overloaded as a condition (because of ambiguity with anonymous calls, eg:void a () and int A (), if a () is called, ambiguity occurs)

The 3.main method can also be overloaded

Overwrite: Subclasses override the method of the parent class, requiring the method name to be exactly the same as the parameter type (the parameter cannot be a subclass), the return value and the exception is smaller or the same as the parent class (that is, the subclass of the parent class), the access adornment transmitting the parent class is large or the same

Two with two small one big

Note: A subclass instance method cannot overwrite a static method of a parent class, nor can a subclass's static method overwrite an instance method of the parent class (compile-time error), summarized as the method cannot cross-overwrite

Hide: When a parent class or subclass has a property or a method with the same name, the property or method form of the parent class is missing, and it actually exists.

Note: When a hidden event occurs, the declared type is a class that invokes the property or method of the corresponding class without dynamic binding

Method hiding has only one form, that is, the parent class and subclass have the same static method

Properties can only be hidden and cannot be overwritten

Subclass instance variable/Static variable can hide instance/static variable of parent class, summarize as variable can be cross-hidden

The difference between hiding and overwriting:

Properties that are hidden are accessed by attributes in the parent class after the child class is cast to the parent class

Overridden method that is called by the subclass itself after the subclass is cast to the parent class

Because overrides are dynamic bindings, which are constrained by the RTTI (run time type identification, runtime types check), hidden from rtti constraints, summarized as Rtti only for overrides, not for hidden

Special cases:

1.final modified properties can be hidden, but cannot be assigned value, that is, cannot be assigned by =, the web says the final property cannot be modified, this is not accurate, because the reference type of the variable with the final decoration, it just cannot be pointed to other objects, but can change its own value, You can use the ArrayList test, the final property can be initialized at run time, but the initialization statement cannot occur

2.final modified methods cannot be overwritten and can be overloaded

3.final-Modified classes cannot be inherited

The 4.private method implicitly adds the final

The difference between overloading, overwriting, and hiding in Java

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.