Overload (overload) and overlay (override) in PHP and Java introduce _php tips

Source: Internet
Author: User
Tags instance method
Overload: In the same class, the function name, return value or parameter type, the number of different is called overload.
Overwrite: A function with the same name, with the return value type, called overlay. refers to the child class's overwrite of the method in the parent class.
PHP does not support method and operator overloading. Java does not support operator overloading (but "+" is actually an operator overload).
Copy Code code as follows:

<?php
Class Father {
Public Function fmeth1 () {
echo "Fmeth1 () ...<br>";
}
Public Function Fmeth1 ($str 1) {
echo "Fmeth1 () with $str 1...<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.

Case of Java (overload) overload and (override) Overwrite Pee

In the Java language Specification, a method's characteristics include only the name of the method, the number and type of parameters, and not the return type of the method, the name of the parameter, and the exception thrown out. When the Java compiler checks the overload of a method, it determines whether the two methods are overloaded methods based on these conditions. However, when the Java compiler checks the substitution of methods, it further checks that the return type of the two methods (the branch supertype and subtype) is the same as the thrown exception.

Question No:3
Copy Code code as follows:

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

Which two are valid in a class of 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-035 of the problem, the standard answer is A,C

A is the override,access from the protected--->public widened and is therefore correct.
B,d is also override,b from the protected--->private narrowed, and D's return type has changed, so all errors.
C is the width of the overload,access and return type does not matter, so is correct.
E is override, but static is added because static method cannot hide the instance method from super class. So it's wrong.
So choose AC.
Subclass inherits the parent class and overrides the parent method called override-rewrite, overwrite, override
Subclasses have multiple identical method names, but the parameters are different, called overload-Heavy (Zhong) load, overload


Overloads are:
Occurs when multiple methods have the same name and contain different parameters
So the different calls to the arguments actually call different methods
It can also be understood that there are actually two methods, the names are the same, the parameters are different!


Overlay (OVERWRITE) Note

Cannot reduce the "visibility of the original method"

The return type cannot be overridden by a method

Overload (Overload) Note
Overloads can be made only by different parameters

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.