The difference between overload and override in Java

Source: Internet
Author: User
Tags modifiers

1. Method overload (Overload)
Concept: Simply put: Method overloading is a variety of implementations of the same function of a class, whichever way it is based, depending on the parameters given by the caller.
Note:
(1) The method name is the same
(2) The parameter type, number and order of the method are not at least one different
(3) The method return type can be different
(4) The modifiers of the method can be different
If only the return type is not the same, the overload cannot be formed
If you just control access modifiers differently, you cannot make an overloaded
The overloaded method is to change the type of the return value.
2. Method coverage (override)
Concept: Simply: The method overlay is a subclass has a method, and the parent class of a method of the name, return type, parameters, then we say that the subclass of this method overrides the parent class of that method.
Note: There are many conditions for method coverage, the general said there are two points must be noted:
(1) The return type of the subclass's method, the parameter, the method name, the return type of the parent class method, the parameter, the method name is exactly the same, or the compilation error occurs.
(2) The subclass method cannot narrow the access rights of the parent class method (which can be reversed)

Cases:

Overrride instance

Class a{

public int Getval () {

return (5);

}

}

Class B extends a{

public int Getval () {

return (10);

}

}

public class Override {

public static void Main (string[] args) {

b b = new B ();

A a= (a) b;//to cast B to a type

int X=a.getval ();

SYSTEM.OUT.PRINTLN (x);

}

}

Overload instance

Package Com.guonan;

Demostrate method Voerloading.

Class Overloaddemo {

void Test () {

System.out.println ("NO parameters");

}

void Test (int a) {

System.out.println ("A:" +a);

}//end of overload test for one integer parameter.

void Test (int a, int b) {

System.out.println ("A and B:" +a+ "" +b);

}

Double Test (double a) {

System.out.println ("Double A:" +a);

return a*a;

}

}

Class overload{

public static void Main (string[] args) {

Overloaddemo ob = new Overloaddemo ();

Double result;

Ob.test ();

Ob.test (10);

Ob.test (10, 20);

result = Ob.test (123.25);

SYSTEM.OUT.PRINTLN ("Result of Ob.test" (123.25): "+result");

}

}

Related Article

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.