Method of Overriding and Overloading

 

Overloading: providing more than one method with the same name but with different signatures to distinguish them.

 

Overriding a method: replacing the superclass's implementation of a method with one of your own. The signatures must be identical, but the return type can vary in a particular way: 

    1)If the return type is a reference type then the overriding method can declare a return type that is a subtype of that declared by the superclass method.

    2)If the return type is a primitive type, then the return type of the overriding method must be identical to that of the superclass method. It is an error if two methods differ only in return type and the compiler will reject your class.

    The overriding methods have their own access specifiers. A subclass can change the access of a superclass's methods, but only to provide more access. A method declared protected in the superclass can be redeclared protected (the usual thing to do) or declared public, but it cannot be declared private or have package access. Making a method less accessible than it was in a superclass would violate the contract of the superclass, because an instance of the subclass would not be usable in place of a superclass instance.

    The overriding method is also allowed to change other method modifiers. The synchronized, native, and strictfp modifiers can be freely varied.

    The overriding method can, however, be made abstract, even though the superclass method was not.

    Also, the overriding method's throws clause can be different from that of the superclass method's as long as every exception type listed in the overriding method is the same as or a subtype of the exceptions listed in the superclass's method.

    Static members within a class whether fields or methods cannot be overridden.

 

 

Preference: 《The Java Programming Language, the Fourth Edition》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值