Thorough learning of overrides and overloads in the Java language

Source: Internet
Author: User
Tags comments implement inheritance interface return access

First see these two words and no special feeling, but a long time, but found the book for a while with override, and then use overload, make me confused. So I made a summary, I hope to be the same as I to the two concepts of ambiguous users have a help.
Override can be translated as overlay, literally knowing that it is overriding a method and rewriting it in order to achieve different roles. The most familiar overlay for us is the implementation of the interface method, in which the method is generally declared, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class in the inheritance. The following points should be noted in the overlay:
1. The marking of the covering method must match the mark of the method covered, so as to reach the effect of coverage;
2. The return value of the overridden method must be consistent with the return of the overridden method;
3. The exception thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4, the overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.
Overload may be more familiar to us, can be translated as overloading, it means that we can define some of the same name methods, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, to select the appropriate method to execute. The following are some things to note when using overloads:
1. When using overloads, you can only pass different parameter styles. For example, different parameter types, different number of parameters, different order of parameters (of course, several parameter types within the same method must not be the same, for example, can be fun (int, float), but not fun (int, int));
2, can not be accessed by Access rights, return type, thrown by the exception to overload;
3, the method of the exception type and number does not affect the overload;
4. For inheritance, if a method is priavte in the parent class, then it cannot be overloaded in subclasses, and if defined, it simply defines a new method and does not achieve the overload effect.
The following is a test program for override and overload, where the comments are code that produces a compile error, and we remove the comments to see what happens at compile time.
Files for overload test: Overloadtest.java
public class Overloadtest {
The following methods are used to verify that a method overload can be made by defining the number of different parameter types and parameters.
public void Fun () {
System.out.println ("Method fun in Overloadtest, no parameter");
}

public void fun (float f) {
System.out.println ("Method fun in Overloadtest, parameter Type:float");
}

public void Fun (int i) {
System.out.println ("Method fun in Overloadtest, parameter Type:int");
}

public void Fun (int i1, int i2) {
System.out.println ("Method fun in Overloadtest, parameter type:int, int");
}

The following two methods are used to verify that a method overload can be performed by defining different order of parameters.
Note: The parameters here are certainly not the same type, otherwise the order is meaningless.
public void fun1 (int i, float f) {
System.out.println ("Method fun1 in Overloadtest, sequence of parameters is:int, float");
}

public void fun1 (float f, int i) {
System.out.println ("Method fun1 in Overloadtest, sequence of parameters is:float, int");
}

The following two methods are used to verify the effect of the exception thrown by the method on overloading.
Neither the type of exception nor the number of exceptions has any effect on overloading.
public void Fun2 () throws TestException {
System.out.println ("Fun2 in Overloadtest, exception:testexception");
}

public void fun2 (int i) throws TestException, TestException1 {
System.out.println ("Fun2 in Overloadtest, Exception:testexception, TestException1");
}

public void fun2 (float f) throws Exception {
System.out.println ("Fun2 in Overloadtest, exception:exception");
}

You cannot overload the fun method by throwing an exception type.
public void Fun (int i) throws Exception {
System.out.println ("Method fun in Overloadtest, parameter type:int, exception:exception");
//}

The fun method cannot be overloaded by a return value.
public boolean fun (int i) throws Exception {
System.out.println ("Method fun in Overloadtest, parameter Type:int, exception:exception, Return:boolean");
return true;
//}

private void Fun3 () {}

Cannot overload with different access rights
public void Fun3 () {}

public static void Main (string[] args) {
This just defines the instance of Overloadtest, so test does not call the
The method in the OverloadTest1.
Overloadtest test = new OverloadTest1 ();
This defines an instance of OverloadTest1, because OverloadTest1 is overloadtest
, so test1 will call the method in Overloadtest.
OverloadTest1 test1 = new OverloadTest1 ();

try {
int i = 1, j = 2, M = 3;

The OverloadTest1 fun method is not invoked here
Test.fun (I, M, j);
Test1.fun (i, J, M);
Test1.fun ();
This call does not execute because FUN3 () access rights in Overloadtest are PRIAVTE
Test1.fun3 ();
TEST1.FUN3 (i);
catch (Exception e) {}
}
}

Class OverloadTest1 extends overloadtest{
Overloading fun in subclasses
public void Fun (int i, int m, int n) {
System.out.println ("Overload fun1 in OverloadTest1, parameter type:int, int, int");
}

This is not an overload of the method in the parent class, just a new method.
public void fun3 (int i) {
System.out.println ("fun2 in OverloadTest1");
}
}

Files for override test: Overridetest.java
public class Overridetest {
public void Fun () throws TestException {
System.out.println ("Method fun in Overridetest");
}

private void Fun1 () {
System.out.println ("Method fun1 in Overridetest");
}

public static void Main (string[] args) {
Overridetest test = new OverrideTest1 ();
try {
Test.fun ();
Test.fun1 ();
catch (Exception e) {}
}
}

Class OverrideTest1 extends overridetest{
The following normal override
public void Fun () throws TestException2 {
System.out.println ("Fun in OverrideTest1");
}

You cannot override a method in a parent class because it defines different exception types and
The return value.
public int Fun () throws TestException1 {
System.out.println ("Method fun in Test");
return 1;
//}

You cannot override a method in a parent class because it throws an illegal range from the parent class
A larger exception.
public void Fun () throws Exception {
System.out.println ("Fun in OverrideTest1");
//}

This method does not override the Fun1 method in the parent class, because the method
The parent class is a private type, so this is just the equivalent of defining a new method.
public void Fun1 () {
System.out.println ("Method fun1 in Test");
}
}

Class TestException extends exception{
Public testexception (String msg) {
Super (MSG);
}
}

Class TestException1 extends TestException {
Public TestException1 (String msg) {
Super (MSG);
}
}

Class TestException2 extends TestException {
Public TestException2 (String msg) {
Super (MSG);
}
}



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.