Method overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of polymorphism between parent and subclasses, while overloading is a manifestation of polymorphism in a class.
If a method defined in a subclass has the same name and parameters as its parent class, we say the method is overriding. When an object of a subclass uses this method, it will call the definition in the subclass. For it, the definition in the parent class seems to be "shielded".
If multiple methods with the same name are defined in a class, and they may have different numbers of parameters, different parameter types, or different parameter orders, it is called method overloading. It cannot be overloaded by access permissions, return types, or thrown exceptions.
1. Method overload (overload)
Concept: To put it simply: method overloading is multiple implementation methods of the same function of a class. Which method is used depends on the parameters given by the caller.
Things to note:
(1) The method names are the same (2) The parameter types, number, and order of the methods are not different in at least one of them (3) The return types of the methods can be different (4) The modifiers of the methods can be different. If only the return types are different, it cannot constitute If the overloading only has different control access modifier symbols, it cannot constitute an overloading.
Overloaded method can change the type of return value.
2. Method override (override)
Concept: To put it simply: method coverage means that the subclass has a method that has the same name, return type, and parameters as a method of the parent class. Then we say that the method of the subclass overrides the method of the parent class.
Note: There are many conditions for method coverage. Generally speaking, there are two points that you must pay attention to:
(1) The return type, parameters, and method name of the subclass method must be exactly the same as the return type, parameters, and method name of the parent class method, otherwise a compilation error will occur.
(2) Subclass methods cannot reduce the access rights of parent class methods (the reverse is possible)
example:
Overrride instance