1. About subclass methods overriding the parent class method follows the rule of "two and two small and one big"
Two identical: (1) method name (2) formal parameter list
Two small: (1) Subclass the return value type is smaller than the parent class or equal (2) the exception thrown by the subclass declaration should be smaller or equal than the parent class
One large: (1) Subclass permissions are larger or equal than the parent class
The above rules are easy to understand:
It is important to note how to understand that the subclass return type is smaller or equal than the parent class.
I believe a lot of beginners, like me, will understand that the scope of int is smaller than the long range, then whether you can override the parent class method, the original parent method return value long to int?
The answer is: No.
Cause: The return value type is smaller than the parent class, the return value type must be a parent-child relationship , and there is no parent-child relationship between the basic types.
The following is a code example: ( Note that student is a subclass of person)
Class A {
Public Person Test (int x) {
return null;
}
}
Class B extends A {
Public Student Test (int x) {
return null;
}
}
Class Person {
}
Class Student extends Person {
}
Deepen the understanding of Java overriding parent class methods