Java Basics: Polymorphism (overloading and rewriting)

Source: Internet
Author: User

(1) Domain and static methods

Remember that " only normal method calls are Polymorphic ".

The domain and static methods are not: Access to the domain has been parsed and bound during compilation. And if a method is static, there is no polymorphism.


(2) Writing a constructor has a guideline:

* Use the simplest possible method to get the object into a normal state and try to avoid calling other methods.

* methods that can be safely called within the constructor are only those final methods (private default final), because they can't be overwritten.


(3) polymorphic concept:

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

These methods have the same name because their final function and purpose are the same, but because they may encounter different situations when they complete the same function, it is necessary to define a method with different specific content to represent a variety of concrete implementation forms.


(4) Java provides two polymorphic mechanisms: overloading and rewriting.

The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism.

Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class.

= " If you define a method in a subclass that has the same name and parameters as its parent, we say that the method is overridden (overriding). when an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked".

= " if more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, it is called a method overload (overloading). the overloaded method is to change the type of the return value (that is, only the return value is not overloaded). Instead , the parameter names are the same, as long as the types are overloaded, even if they are inherited from each other.


Java Basics: Polymorphism (overloading and rewriting)

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.