Java中Overload和Override的区别(动态多态和静态多态)

本文详细介绍了Java中Overload(重载)和Override(重写)的概念,区别以及它们在多态中的实现。重载发生在同一类中,方法名相同但参数列表不同;重写发生在子类中,子类提供与父类同名且参数列表相同的方法。动态多态(Override)在运行时根据对象的实际类型确定调用哪个方法,静态多态(Overload)在编译时由引用类型决定调用哪个方法。
摘要由CSDN通过智能技术生成

转自

Method Signature 标识 method name + method parameter list。

Definitions

  1. Overriding means having two methods with the same method name and parameters (代表相同的method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

  2. Overloading occurs when two or more methods in one class have the same method name but different parameters(代表不同的method signature).

Overriding vs. Overloading

  1. Overriding(动态多态)The real object type in the run-time, not the reference variable’s type, determines which overridden method is used at runtime. (overriding中,编译后两个方法的签名一样,所以需要通过程序运行时,由调用对象的对象类型决定调用哪个方法,而不是引用的变量类型。)

  2. Overloading(静态多态)In contrast, reference type determines which overloaded method will be used at compile time.(overloading中,编译后多态方法具有不同的签名。)

An Example of Overriding

Here is an example of overriding. After reading the code, guess the output.

class Dog{
    public void bark(){
        System.out.println("woof ");
    }
}
class Hound extends Dog{
    
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值