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.
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安全有效的实现两星期内自动登陆功能
- 【Java】判断文件的后缀名
- Java replaceFirst 和replaceAll 方法
- Spring JdbcTemplate方法详解
- JSP+Servlet+javabean实现登录功能模块
- Eclipse中用git解决冲突 避免每次重新拉代码
- Java 6 JVM参数选项大全(中文版)
- java实现屏幕截图功能并保存截图
- java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET
- spring中context:property-placeholder/元素
- Springmvc返回json 字符串的其他方式
- spring mvc乱码的解决方案
- java观察者模式
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- Java中图片的相对路径
- 【Spring-AOP-学习笔记-5】@AfterReturning增强处理简单示例
- JAVA抽奖的算概率法
- Java执行操作系统命令
- JAVA中String类
- java调用JDBC连接MySQL 以及MySQL日常维护的简单操