java covariance

Covariance means that the type of arguments, return values, or exceptions of overriding methods can be subtypes of the original types.

在Java中不支持arguments(参数)的Covariance
override(继承) -- covariance of return value and/or exception

在继承时,允许 子类方法中“返回值和exception”是父类方法的 “返回值和exception” 的子类,这是override覆盖

在继承时,参数类型即使具有继承关系,那也是两个方法,是overload重载

override例子
class Parent{ 
  Object func(Number n) throws Exception{ 
    ... 
  } 
} 

class Child extends Parent{ 
  String func(Number n) throws SQLException { 
    ... 
  } 
}



overload例子
public class C { 
public void func(Integer n){ 
} 
public void func(String n){ 
} 


参数的Covariance,属于overload
class Parent{ 
  Object func(Number n){ 
    ... 
  } 
} 

class Child extends Parent{ 
  Object func(Integer i)  { 
    ... 
  } 
} 
类的方法表
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 

你可能感兴趣的:(java)