Home  >  Article  >  Backend Development  >  Introduction to overload and override in PHP and JAVA_PHP Tutorial

Introduction to overload and override in PHP and JAVA_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:181490browse

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();
?>

where The fmeth1 method in the parent class cannot be overloaded.

A brief explanation of overloading and override coverage 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 names of the parameters, and the exceptions thrown. When the Java compiler checks the overloading of methods, 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 the 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; }

310 For the question in -035, the standard answer is A, 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 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 wrong.
So choose AC.
A subclass that inherits a parent class and overrides its methods is called override - rewrite, overwrite, override
A subclass that has multiple methods with the same name but different parameters is called overload - heavy (zhong) load, Overloading


overloading is:
Produced 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 but different parameters!


Overwrite (OVERWRITE) Note that

cannot reduce the "visibility" of the original method

Different return types cannot constitute method override

Overload (OVERLOAD) Note
Only different parameters can constitute overloading

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325084.htmlTechArticleOverloading: In the same class, the function name is the same, the return value or parameter type is different, but the number is called Overload. Override: Functions with the same name, the same return value type, and the same parameters are called overrides. Refers to...
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