Several points of attention in Java methods Override and overload

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.