The difference between overloading and rewriting? ? ?

1. Overload : (overload)

Insert picture description here

(1) The number is different :
Insert picture description here

public class OverLoad {
    
    
    public static int add (int a,int b) {
    
    
        return a + b;
    }
    public static int add (int a,int b,int c) {
    
    
        return a + b + c;
    }
    public static void main(String[] args) {
    
    
        int a = 10;
        int b = 20;
        System.out.println(add(a,b));
        int c = 12;
        System.out.println(add(a,b,c));
    }
}

(2) Different types :

Insert picture description here

public class OverLoad {
    
    
    public static int add (int a,int b) {
    
    
        return a + b;
    }
    public static double add (double x,double y) {
    
    
        return x + y;
    }
    public static void main(String[] args) {
    
    
        int a = 10;
        int b = 20;
        System.out.println(add(a,b));
        double c = 12.5;
        double d = 13.5;
        System.out.println(add(c,d));
    }
}

(3) The order is different :
Insert picture description here

public class OverLoad {
    
    
    public static double add (double a,int b) {
    
    
        return a + b;
    }
    public static double add (int x,double y) {
    
    
        return x + y;
    }
    public static void main(String[] args) {
    
    
        double a = 10.7;
        int b = 20;
        System.out.println(add(a,b));
        int c = 12;
        double d = 13.5;
        System.out.println(add(c,d));
    }
}

2. Rewrite : (override)

(1) The method name is the same ;
(2) The parameter list is the same (the number of parameters, the type of the parameter)
(3) The return value should also be the same

But there are a few points to note:
(1) The method to be rewritten cannot be modified by private
(2) The method modified by finale cannot be rewritten
(3) The method needs to be rewritten, access modifier qualifier, The access modifier of the subclass must be greater than or equal to the access modifier of the parent class

Guess you like

Origin blog.csdn.net/qq_45658339/article/details/108822052