Current location - Recipe Complete Network - Complete breakfast recipes - How to distinguish between overloading and overwriting of methods in Java?
How to distinguish between overloading and overwriting of methods in Java?
Overload: It happens inside a class, and it is for methods, not domains. If the signatures of the methods are the same, but the number of parameters or the types of parameters are different, it constitutes an overload. However, the return type is not considered, that is to say, if the return type is different, it will not be overloaded. Override: It occurs between the parent class and the subclass, and it is for instance methods (that is, non-static methods) rather than domains. To constitute an override, the following conditions must be met: 1, and the static method in the parent class cannot be overridden; 2. The final method in the parent class cannot be overwritten; 3. The signature, parameter number and parameter type of the method should be the same; There are two points to note about the return type: 1) If the return type is a reference type, the return type of the override method can be declared as a subtype of the return type declared by the parent class method; 2) If the return type is a basic type, the return type of the override method must be the same as that of the parent method; Source: (/s/blog _ 5c4532e50100bqbl.html)-Overloading, Covering and Hiding in Java _ Village Head _ Sina Blog's Description of Method Parameters: Subclasses can modify this parameter regardless of whether the parameter in the parent method is final or not; 4. The access right of the subclass method should be greater than or equal to that of the parent class; 5. Subclass methods can change some method modifiers, such as synchronized, native and strictfp. 6. The throws clause of a subclass method can be different from that of a parent class method, and each exception type listed by it should be the same as that of the parent class or a subclass of the parent class exception type; Hidden: It occurs between the parent class and the subclass, and it is only for static members (static methods and static fields). Note: Class methods declared as final cannot be hidden. When an instance method is overwritten by a subclass method, the actual called method is determined at execution time. When a method of a class is hidden by a method of a subclass, the method actually called is determined at compile time.