About method overrides in Java:
1, the method in the subclass has the same return type as the method in the parent class, the same method name, the same argument list
2. The access level of a method in a subclass cannot be lower than the access level of the method in the parent class (that is, the modified private protected public level is from low to high) before the method
3, the scope of the exception thrown by the method in the subclass cannot be greater than the scope of the exception thrown by the method in the parent class (that is, the subclass can not throw an exception, or the thrown exception is a subclass of the exception thrown by the parent class)
Class A {
public int Getlen () ... {
return 1;
}
}
public class B extends A {
public float Getlen () ... {
return 2;
}
}
This is neither overload nor override. ... ...
Covariance.
Covariance means that the type of arguments, return values, or exceptions of overriding methods can is subtypes the Ori Ginal types.
Java
Exception covariance has been supported since the introduction. Return type covariance was implemented in the Java programming language version J2SE 5.0. Parameter types have to being exactly the same (invariant) for method overriding, otherwise the ' method ' overloaded with a P Arallel 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 {
...
}
}
It's called override. Because the return value of the child Func method and exception both the return value of parent Funct method and the exception subclass.
SQLException extends Exception (since-version)
String extends Object, (since J2SE 5.0)
So, this is 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) {
...
}
}
This is overload. Because Java does not support covariance of method parameter types.
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