Home >Backend Development >PHP Tutorial >allowoverride Introduction to overload and override in PHP and JAVA

allowoverride Introduction to overload and override in PHP and JAVA

WBOY
WBOYOriginal
2016-07-29 08:48:091145browse

Overloading: In the same class, functions with the same name but different return values ​​or parameter types are called overloading.
Override: Functions with the same name, the same return value type, and the same parameters are called overrides. It refers to the overriding of methods in the parent class by the subclass.
PHP does not support method and operator overloading. JAVA does not support operator overloading (but "+" is actually an operator overloading).

Copy code 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 It cannot be overloaded.
Explanation of overload and override cases in Java
In the Java language specification, the characteristics of a method only include the name of the method, the number and type of parameters, but not the return type of the method, the parameters of the parameters The name and exception thrown. When the Java compiler checks method overloading, it will determine whether two methods are overloaded methods based on these conditions. But when the Java compiler checks the substitution of the method, it will further check whether the return type and exception thrown by the two methods (supertype and subtype) are the same.
QUESTION NO: 3

Copy code The code is as follows:


class A {
protected int method1(int a, int b) { return 0; }
}


Which two are valid in a class that extends class A? (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; }
For the question in 310-035, the standard answers are A and C
A is override, and access has been broadened from protected--->public, so it is correct.
B and D are also overrides. B has become narrower from protected--->private, and the return type of D has changed, so they are all wrong.
C is overload, the width and return type of access don’t 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 wrong.
So choose AC.
A subclass that inherits a parent class and overrides a method of the parent class is called override - override, overwrite, override.
A subclass that has multiple methods with the same name but different parameters is called overload - heavy (zhong) load, overload
overload It is:
Occurs when multiple methods have the same name but contain different parameters
Then calls with different parameters actually call different methods
It can also be understood that there are actually two methods with the same name and different parameters!
Overwrite (OVERWRITE) Note
cannot reduce the visibility of the original method
Different return types cannot constitute a method override
Overload (OVERLOAD) Note
Only different parameters can constitute an overload

The above is an introduction to allowoverride overload and override in PHP and JAVA, including the content of allowoverride. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn