关于JAVA中的方法重写:
1、子类中的方法与父类中的方法有相同的返回类型,相同的方法名称,相同的参数列表
2、子类中的方法的访问级别不能低于父类中该方法的访问级别(即 方法前的修饰 private protected public 级别从低到高)
3、子类中方法抛出的异常的范围不能大于父类中方法抛出的异常的范围(即 子类可以不抛出异常,或者抛出的异常是父类抛出的异常的子类)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
// 这既不是overload也不是override。
covariance.
Covariance means that the type of arguments, return values, or exceptions of overriding methods can be subtypes of the original types.
Java
Exception covariance has been supported since the introduction of the language. Return type covariance is implemented in the Java programming language version J2SE 5.0. Parameter types have to be exactly the same (invariant) for method overriding, otherwise the method is overloaded with a parallel definition instead.
(1) override -- covariance of return value and/or exception
class Parent{
Object func(Number n) throws Exception{
...
}
}
class Child extends Parent{
String func(Number n) throws SQLException {
...
}
}
这叫做override。因为child func method的返回值和Exception都parent funct method的返回值和Exception的子类。
SQLException extends Exception (since first version)
String extends Object, (since J2se 5.0)
所以,这是overrider.
parent vtable
Entry 1: Object func(Number n) throws Exception of Parent
child vtable
Entry 1: String func(Number n) throws SQLException of Child
(2)overload - no support covariance of parameter type
class Parent{
Object func(Number n){
...
}
}
class Child extends Parent{
Object func(Integer i) {
...
}
}
这是overload。因为java不支持method参数类型的covariance。
parent vtable
Entry 1: Object func(Number n) of Parent
child vtable
Entry 1: Object func(Number n) of Parent
Entry 2: Object func(Integer i) of Child