Getting started in the Java language Tutorial (14): Method overload and method overrides in the Java language

Source: Internet
Author: User
Tags abs exception handling

Most of the time, there are several similar methods in a Java class. As in the math class, there are multiple methods for calculating absolute values, because the number of different data types is calculated differently. For example, for Float,int, long,double various types of calculation methods are different, then the math class should be designed 4 methods to calculate the absolute value of each type. In this case, the best way to do this is to design 4 methods with the same name but different parameters, as shown below (see API Help documentation):

ABS (double A)
          Returns the absolute value of a double value.
ABS (float a)
          Returns the absolute value of a float value.
ABS (int a)
          Returns the absolute value of an int value.
ABS (Long a)
          Returns the absolute value of a long value.

This is the method overload, English is overloading. Method overloads are defined as: In a Java class, multiple methods with the same name, but different parameters, are called method overloads. Method overloading mainly improves the readability of code and facilitates the users of the method.

When it comes to method overloading, there is a particularly confusing notion of method overrides, also called method rewriting, in English for overridding. Method overrides are concepts that are only inherited. Start with the code first.

For example, the parent class employee has the following methods:

public void Setsalary (double salary,double rate) {
       this.salary = salary* (1+rate);
}

Suppose that the business logic is in the subclass sales of employee, but the implementation is somewhat different. Suppose the implementation is as follows:

This.salary = salary* (1+2*rate);

At this point, you can define a method with the same name as the parent class in the child class sales:

public void Setsalary (double salary,double rate) {
       this.salary = salary* (1+2*rate);
}

This is the method overlay. That is, a subclass has a method that has the same parameter return as the parent class, but the logic of the method body is somewhat different, and we call it a subclass that overrides a method of the parent class. After overriding the parent class method, such as by:

Sales S=new sales ();

S.setsalary (6000,0.1);

The Setsalary method of the subclass is invoked at this point, that is, the object of the subclass sales, and no longer calls the Setsalary (double,double) method in the employee because this method has been overridden by sales to its own setsalary ( double,double).

Beginners see this definition and often have a question. Since the Setsalary method of the sales class has a different logic, why not define a method that has the same name as the parent class? such as:

public void Setsalessalary (double salary,double rate) {
       this.salary = salary* (1+2*rate);
}

If you define this, you can complete the new logic as well. So does the method coverage make sense?

If we define the Setsalessalary method in sales, then sales also have the Setsalary method of employee:

public void Setsalary (double salary,double rate) {
       this.salary = salary* (1+rate);
}

In this way, it is wrong to cause a sales class to have an act that the sales object should not have.

The concept of method overlay is summarized as follows:

1, the method in the subclass is the same as the method of the parent class, the same parameter, the same return value

2, the subclass of the method access rights can not be reduced, can only be equated or expanded

3. The subclass method cannot throw more exception types than the parent class method (exception handling will be explained in later articles)

Method overload, method overrides are two confusing concepts, but they are completely unrelated. Overloading refers to a class in which multiple methods with the same name differ, and overrides are concepts that exist in the parent class and subclass.

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.