重载和重写的区别???

1、 重载 :(overload)

在这里插入图片描述

(1)个数不同
在这里插入图片描述

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) 类型不同

在这里插入图片描述

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) 顺序不同
在这里插入图片描述

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、重写 : (override)

当子类发现父类方法不适合自己时,需要重写父类的方法

(1)方法名相同
(2)参数列表相同(参数的个数,参数的类型)
(3)返回值类型相同或更小、抛出的异常更小
(4)访问权限相同或更大

但是有几点需要注意:
(1)要重写的方法,不能是 private 所修饰的
(2)被 finale 所修饰的方法,不能被重写
(3)需要重写方法的,访问修饰限定符,子类的访问修饰限定符一定要大于或者等于父类的访问修饰限定符

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值