Analysis of the difference between Java overload and override

  • 2020-04-01 01:08:00
  • OfStack

Rewriting method (Overriding) and heavy (Overloading) is a Java polymorphism of different performance. Rewrite (Overriding) is a kind of polymorphism between parent and children, and overloaded (Overloading) is a kind of polymorphism in a performance.
If a method defined in a subclass and its parent class have the same name and parameters, we say that this method be rewritten (Overriding). When an object in a subclass USES this method, it invokes the definition in the subclass, to which the definition in the parent class is "masked."
If multiple methods with the same name are defined in a class that either have different number of arguments or have different parameter types or have different order of arguments, it is called Overloading of the method. Cannot be overloaded with access rights, return types, or exceptions thrown.

1. Method overload
Concept: simply put: method overloading is the multiple implementations of the same function of a class, depending on the parameters given by the caller.  
Notes:
(1) the method name is the same
(2) at least one parameter type, number and order of the method is different
(3) method return types can be different
(4) method modifiers can be different
If only the return type is different, it cannot constitute an overload
If only control access modifier is not the same, also can not constitute an overload
Overloaded methods can change the type of return value.
2. Method override
Concept: simply put: method override means that a subclass has a method with the same name, return type, and arguments as a method of the parent class, so we say that the method of the subclass overrides the method of the parent class.
Note: there are many conditions for method coverage. In general, there are two points to be paid attention to:
(1) the return type, parameter and method name of the method of the subclass should be exactly the same as the return type, parameter and method name of the method of the parent class, otherwise the compilation will be wrong.
(2) subclass methods cannot narrow the access rights of parent methods (the other way is ok)
Ex. :
Overrride instance
 
class A{ 
public int getVal(){ 
return(5); 
} 
} 
class B extends A{ 
public int getVal(){ 
return(10); 
} 
} 
public class override { 
public static void main(String[] args) { 
B b = new B(); 
A a= (A)b;//Convert b to the type of A
int x=a.getVal(); 
System.out.println(x); 
} 
} 
Overload The instance  
package com.guonan; 
//Demostrate method voerloading. 
class OverloadDemo { 
void test(){ 
System.out.println("NO parameters"); 
} 
void test(int a){ 
System.out.println("a:"+a); 
}//end of Overload test for one integer parameter. 
void test(int a, int b){ 
System.out.println("a and b:"+a+" "+b); 
} 
double test(double a){ 
System.out.println("double a:"+a); 
return a*a; 
} 
} 
class Overload{ 
public static void main(String[] args) { 
OverloadDemo ob = new OverloadDemo(); 
double result; 
ob.test(); 
ob.test(10); 
ob.test(10, 20); 
result = ob.test(123.25); 
System.out.println("Result of ob.test(123.25):"+result); 
} 
} 

Related articles: