Differences between "JAVA" overloads and Overrides

Source: Internet
Author: User

Rewrite (overriding)

Rewrite rules

1. Parameter list: must exactly match the parameter list of the overridden method.
2. Return type: Must be exactly the same as the return type or subtype declared in the overridden method in the superclass
3. Access level: must not be stronger than the overridden method, can be weaker than the overridden method.
4. Non-check exception: The override method can throw any non-checked exception, regardless of whether the overridden method declares the exception.
5. Check for exceptions: The overriding method must not throw a new check exception, or a check exception that is wider than the check exception declared by the overridden method
6. Cannot override the method marked as Final,static

which

1. The polymorphism between the parent class and the subclass, redefining the function of the parent class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). In Java, subclasses can inherit methods from the parent class without having to rewrite the same method. But sometimes subclasses do not want to inherit the parent class's methods, but want to make some changes, which requires a method of rewriting. Method overrides are also called method overrides.

2. If a method in a subclass has the same method name, return type, and parameter table as a method in the parent class, the new method overwrites the original method. If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.

3. The access adornment permission of the subclass function can not be less than the parent class; 

Overloading (overloading)

Rules for overloaded methods:
1. Parameter list: The overloaded method must change the parameter list.
2. Return type: You can change the return type.
3. Modifier: modifier can be changed
4. Exception: You can declare a new or broader exception.

which

1. Method overloading is a means of allowing classes to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time , with different number/types of parameters. Overloaded overloading is a representation of polymorphism in a class.

2. Java's method overloading is that you can create multiple methods in a class that have the same name but have different parameters and different definitions. The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.

3. When overloaded, the method name is the same, but the parameter type and number are different, and the return value type can be the same or different. The return type cannot be used as a distinguishing criterion for overloaded functions.

Summary:

Overloads and overrides (Overrides).

The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding).

When an object of a subclass uses this method, the definition in the subclass is called, and the definition in the parent class is "masked", and if the subclass's method name and parameter type and number are the same as the parent class, the subclass's return value type must be the same as the parent class, and if multiple methods with the same name are defined in a class, They either have a different number of arguments or have different parameter types, which are called overloads of the Method (overloading). The overloaded method is to change the type of the return value. That is, the overloaded return value types can be the same or different.

Differences between "JAVA" overloads and Overrides

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.