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
1. Class A {
2. protected int method1 (int a, int b) {return 0;}
3.}
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