Java - Method Overloading vs Method Overriding

转载 2015年07月09日 16:34:28

http://javarevisited.blogspot.de/2011/12/method-overloading-vs-method-overriding.html

http://javarevisited.blogspot.sg/2014/03/covariant-method-overriding-of-java-5.html

Sometimes I can't open this page without proper proxy configuration, so I have to copy the content here.


Method overloading and method overriding is based on Polymorphism in Java.


How to Overload a Method in Java

To overload a Java method just changes its signature. Just remember in order to change signature you either need to change number of argument, type of argument or order of argument in Java if they are of different types. Since return type is not part of method signature simply changing return type will result in duplicate method and you will get compile time error in Java.

How to Override a Method in Java

When you override a method in Java its signature remains exactly same including return type. JVM resolves correct overridden method based upon object (注意是基于对象,不是基于类型。哪个对象调用了overriden方法,那个对象所在的类中的overriden方法才会被调用) at run-time by using dynamic binding in Java.
This is very useful technique to modify behavior of a function in Java based on different implementation. equals(), hashcode() and compareTo() methods are classic example of overridden methods in Java.

Another important point is that you can not override static method in Java because they are associated with Class rather than object and resolved and bonded during compile time and that’s the reason you cannot override main method in Java. Similar to static, private and final methods are also not overridden in Java.

By the way, as part of overriding best practice, always use @Override annotation, while overriding method from an abstract class or interface.

Rules of Method Overriding in Java

1)  Method signature must be same including return type, number of method parameters, type of parameters and order of parameters

2)  Overriding method can not throw higher Exception than original or overridden method. means if original method throws IOException then overriding method can not throw super class of IOException e.g. Exception but it can throw any sub class of IOException or simply does not throw any Exception. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception.

3)  Overriding method can not reduce accessibility of overridden method , means if original or overridden method is public then overriding method can not make it protected.

相同点:

name of method remains same

Difference between Method Overloading vs Overriding in Java

1) First and most important difference is that,In case of method overloading in Java, signature of method changes while in case of method overriding it remain same.

2) Second major difference is that You can overload method in one class but overriding can only be done on subclass.

3) You can not override static, final and private method in Java but you can overload static, final or private method in Java.

4) Method overloading is resolved during compile time, while method overriding is resolved at runtime. Overloaded method in Java is bonded by static binding and overridden methods are subject to dynamic binding.


Covariant Method Overriding of Java 5

Covariant method overriding helps to remove type casting on client side, by allowing you to return subtype of actually return type of overridden method. Covariant overriding can be really useful, while overriding methods which returns object e.g. clone() method. Since clone() return object every client needs to cast on to appropriate subclass. By using Java 5 covariant overriding, we can directly return subtype instead of object.


相关文章推荐

Java方法重写(overriding method)

基本概念        在Java中,子类可继承父类中的方法,而不需要重新编写相同的方法。但有时子类并不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。方法重写又称方法覆盖。 ...
  • tiana0
  • tiana0
  • 2013年11月17日 13:11
  • 7577

java中的重载(overriding)跟重写(overloading)理解

话说前两天学校来了传说中的“校招”,主要是来宣讲的,对于我们这种普通二本院校,没有那种大公司来的,来了也是打着别人的招牌,我参加了笔试。 哎,虽然都是基础,但是感觉都忘的差不多了,其中一个关于面向对...

在Java中,什么是Overriding?什么是Overloading?

在Java中,什么是Overriding?什么是Overloading? author: ZJ 07-3-13 Blog: [url]http://zhangjunhd.blog.51cto.co...

Java中的方法重写(Overriding)与方法重载(Overloading)

1.定义 Overriding和Overloading对Java编程新手来说是两个比较容易混淆的概念。 方法重载(Overloading)是一个类中定义了多个方法名相同,而他们的参数的数量不同或数...

java中方法的重写(overriding)与重载(overloading)的区别

方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现。重写(Overriding)是父类与子类之间多态性的一种表现,而重载(Overloading)是一个类中多...

Java方法重载(overloading)与方法重写(overriding)

首先我们来讲讲:重载(Overloading)   (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段。多个同名函数同时存在,具有不同的参数个数/类型。   重载Overloading是...

Java学习笔记之重写(Overriding)与重载(Overloading)

更多博文可参考我的个人独立博客:贱贱的梦想 重写(Overriding)重写是子类对父类的允许访问的方法的实现过程进行重新编写!返回值和形参都不能改变。即外壳不变,核心重写! 重写的好处在于子类可...

方法重载(method overloading)

Java视频教程 成员方法重载 学习重载之前,我们来了解一下在java中方法的特征。 在java中,每一个方法都有自己的特征,其特征主要是指方法名以及方法的参数。 void method1...

c#学习笔记之十四,什么是method overloading?

什么是method?函数也.overloading,是过载的意思。为什么会过载呢?因为一个函数,本来后面拖着两个参数的,现在拖着三个参数了,那不是过载是什么? 为什么同一个函数,后面可以跟两个参数,也...
  • CGGUANG
  • CGGUANG
  • 2016年11月21日 05:49
  • 398

MethodHandle VS Method, Java反射及MethodHandle性能测试

从结果来看, Oracle的Java新特性MethodHandle还有很多要优化的地方 import java.lang.invoke.MethodHandle; import java.l...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java - Method Overloading vs Method Overriding
举报原因:
原因补充:

(最多只允许输入30个字)