Overload and override in PHP and Java

Source: Internet
Author: User

Overload: in the same class, function names are the same, return values or parameter types, and different numbers are called overload.
Overwrite: a function with the same name. It is of the same return value type and the same parameter is called overwrite. It refers to the subclass override of methods in the parent class.
PHP does not support methods and operator overloading. Java does not support Operator Overloading (but "+" is actually an operator overload ). CopyCode The Code is as follows: <? PHP
Class father {
Public Function fmeth1 (){
Echo "fmeth1 ()... <br> ";
}
// Public function fmeth1 ($ str1 ){
// Echo "fmeth1 () with $ str1.... <br> ";
//}
}
Class son extends father {
Public Function fmeth1 (){
Echo "fmeth1 () in son... <br> ";
}
}
$ S = new son ();
$ S-> fmeth1 ();
?>

The fmeth1 method in the parent class cannot be overloaded.

Small Example of overload and override in Java

In the Java language specification, features of a method only include the method name, number and type of parameters, but not the return type of the method, parameter name, and thrown exceptions. When the Java compiler checks the method overloading, it determines whether the two methods are overloaded methods based on these conditions. However, when the Java compiler checks replacement of methods, it further checks whether the return type and thrown exception of the two methods (subclasses and subclasses) are the same.

Question No: 3Copy codeThe Code is as follows: Class {
Protected int Method1 (int A, int B) {return 0 ;}
}

Which two are valid in a class that extends Class? (Choose two)
A. Public int Method1 (int A, int B) {return 0 ;}
B. Private int Method1 (int A, int B) {return 0 ;}
C. Private int Method1 (int A, long B) {return 0 ;}
D. Public short Method1 (int A, int B) {return 0 ;}
E. Static protected int Method1 (int A, int B) {return 0 ;}

The standard answer for the question in 310-035 is a, c

A is override, and access is increased from protected ---> public, so it is correct.
B and D are also override, B is narrowed from protected ---> private, and the return type of D is changed, so they are all incorrect.
C is overload, and access width and return type do not matter, so it is correct.
E is override, but static is added because static method cannot hide the instance method from super class. Therefore, it is incorrect.
Select AC.
The Child class that inherits the parent class and overwrites the parent class method is called override -- override, overwrite, Overwrite
The subclass has multiple identical method names, but different parameters are called overload-weight (Zhong) loads, overload

Reload is:
When multiple methods have the same name and contain different parameters
Different methods are called for different parameters.
It can also be understood that there are actually two methods with the same name and different parameters!

Overwrite note

The visibility of the original method cannot be reduced.

Different return types cannot overwrite the method.

overload
an overload can be created only when the parameters are different.

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.