Java polymorphism, overloading, overriding

Source: Internet
Author: User

1. Polymorphism (polymorphism):

Polymorphism refers to the specific type of reference variable defined in the program and the method call emitted by the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable is inverted to the instance object of the class that the reference variable emits, and the method called by which class is implemented. Must be determined by the time the program is running. This is polymorphism. Polymorphism enhances the flexibility and extensibility of the software.

To put it simply: send a message to an object and let the object decide for itself what behavior to respond to. A dynamic method call is implemented by assigning a subclass object reference to a superclass object reference variable.

Polymorphism must have:

A. Base classes and individual subclasses

B. A base class reference that points to the instantiated subclass object.

"Computer Graphics" in the plotting logic is polymorphic, the realization of the point line element management, call the parent class draw method, the last point of the line surface of the child elements to draw.

2. Overwrite (override)

A, the mark of the method of coverage must match with the mark of the method that is covered, can reach the effect of coverage;
b, the return value of the overridden method must be consistent with the return of the overridden method;
C, the overridden method throws an exception that must be the same as the exception that is thrown by the overridden method, or its subclass;
D, the overridden method cannot be private, otherwise only a new method is defined in its subclass and is not overwritten

3. Overloading (overload)

A, you can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must differ, such as can be fun (int, float), but not fun (int, int));
B, cannot be overloaded by Access permission, return type, exception thrown;
C, the exception type and number of methods will not affect the overload;
D, for inheritance, if a method is priavte in the parent class, then it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.

Java polymorphism, overloading, overriding

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.