Differences between overload and override in PHP

Source: Internet
Author: User
This article describes the differences between overload and override in PHP. This article describes the differences between overload and override in PHP. It has good reference value. let's take a look at it together with the small editor.

Override (overwrite, overwrite)

1. the method name, parameter, and return value are the same.

2. the sub-class method cannot reduce the access permission of the parent class method.

3. the subclass method cannot throw more exceptions than the parent method (but the subclass method can not throw an exception ).

4. exists between the parent class and the child class.

5. the method is defined as final and cannot be overwritten.

Overload)

1. there must be at least one different parameter type, number, and order.

2. the method names with different return values cannot be reloaded.

3. it exists in the parent class, child class, and similar class.

Overriding and Overloading are different manifestations of Java polymorphism.

Overriding is a manifestation of polymorphism between parent class and child class, while Overloading is a manifestation of 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, different parameter types, or different order of parameters, this is called Overloading ). It cannot be overloaded by access permission, return type, or thrown exception.

1. Override features

1. the logo of the covered method must match the logo of the covered method to achieve the coverage effect;

2. the returned value of the overwritten method must be the same as that of the overwritten method;

3. The Exception thrown by the overwriting method must be the same as that thrown by the overwriting method, or its subclass;

4. the method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.

2. Overload features

1. you can only use different parameter styles when using overload. For example, different parameter types, different parameter numbers, and different parameter order (of course, several parameter types in the same method must be different, such as fun (int, float ), but cannot be fun (int, int ));

2. it cannot be overloaded by access permission, return type, or thrown exception;

3. the method exception type and quantity do not affect the heavy load;

4. for inheritance, if a method has a priavte access permission in the parent class, it cannot be overloaded in the subclass. if it is defined, it only defines a new method and does not achieve the effect of overloading.

The above is a detailed explanation of the differences between overload and override in PHP. For more information, see PHP Chinese website (www.php1.cn )!

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.