Overload and override in PHP and JAVA _ PHP Tutorial

Source: Internet
Author: User
Overload and override in PHP and JAVA. 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. 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 ).

The code is as follows:


Class Father {
Public function fmeth1 (){
Echo "fmeth1 ()...
";
}
// Public function fmeth1 ($ str1 ){
// Echo "fmeth1 () with $ str1...
";
//}
}
Class Son extends Father {
Public function fmeth1 (){
Echo "fmeth1 () in son...
";
}
}
$ 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: 3

The 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
Only different parameters can constitute an overload.

Bytes. Overwrite: a function with the same name. it is of the same return value type and the same parameter is called overwrite. ...

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.