Overloading of overload and overloading of override are introduced in PHP and JAVA

  • 2020-05-16 06:31:30
  • OfStack

Overloading: in the same class, the function name is 1, return value or parameter type, the number is not 1 is called overload.
Overwrite: function of the same name, same type of return value, same parameter is called overwrite. Refers to the subclass overrides the method in the parent class.
PHP does not support method and operator overloading. JAVA does not support operator overloading (but "+" is actually one operator overloading).
 
<?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 is not overloaded.

Case minimization of (overload) overloading and (override) overrides in java

In the Java language specification, the characteristics of a method include only the name of the method, the number and type of arguments, not the return type of the method, the names of the arguments, and the exceptions thrown. When the Java compiler checks for method overloads, it determines whether two methods are overloaded based on these conditions. But when the Java compiler checks for substitutions of methods, it takes a step forward to check whether the return type and the exception thrown are the same for both methods (branch supertypes and subtypes).

QUESTION NO: 3
 
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 questions 310-035, the standard answer is A, C

A is override,access is from protected-- > public is wider, so it is correct.
B, D are also override,B from protected-- > private narrowed, D's return type changed, so it's all wrong.
C is overload, and access's width and return type don't matter, so it's true.
E is override, but it adds static, because static method cannot hide the instance method from super class.
So AC.
The subclass that inherits the parent class and overrides the parent class method is called override -- overwrite, overwrite, overwrite
Subclasses have many of the same method names, but different parameters, called overload - heavy (zhong) load, overload


Overloading is:
Occurs when multiple methods have the same name but different parameters
So different calls to parameters actually call different methods
It can also be understood that there are actually two methods with the same name and different parameters!


Override (OVERWRITE) note

You cannot reduce the visibility of the original method

Different return types do not constitute method overrides

Overloading (OVERLOAD) note
An overload can be constructed only if the parameters are different

Related articles: