Java basics: Polymorphism (overloading and rewriting)

Source: Internet
Author: User

Java basics: Polymorphism (overloading and rewriting)

(1) domain and static methods

Remember"Only calls of common methods are polymorphism.".

The domain and static methods are not: the access to the domain has been resolved and bound during the compilation process. If a method is static, there is no polymorphism.

 

(2) The constructor has a criterion:

* Use the simplest method possible to make the object normal and avoid calling other methods as much as possible.

* Only the final methods that can be safely called in the constructor (private default final), because they cannot be overwritten.

 

(3) concept of polymorphism:

Polymorphism refers to the coexistence of different methods with the same name in a program.

The reason why these methods have the same name is that they have the same final function and purpose. However, when the same function is completed, different situations may occur, therefore, you need to define methods that contain different specific content to represent multiple specific implementation forms.

 

(4) Java provides two types of polymorphism mechanisms: overloading and rewriting.

Overriding and Overloading are different manifestations of Java polymorphism.

Overriding is a manifestation of the polymorphism between the parent class and the Child class, and Overloading is a manifestation of the polymorphism in a class.

="If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ).When a subclass object uses this method, the definition in the subclass is called. For it, the definition in the parent class is "blocked.

="If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called Overloading ).The Overloaded method can change the type of the returned value (that is, only different return values are not Overloaded ). Even if the parameter names are the same, as long as the types are different, they are overloaded even if they are mutually inherited.

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.