您的位置:首页 > 编程语言 > Java开发

Java - Method Overloading vs Method Overriding

2015-07-09 16:34 441 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: