Overload (overloaded) and override (override, overwrite) in Java

Source: Internet
Author: User

A method overload in Java occurs when two or more methods in the same class have the same method name but different arguments, and the method overload refers to a method in which the subclass and the parent class redefine the parent class, overriding the same method as the original method signature, return value, and parameter.
Overload (Heavy Duty)

    • Overloaded overload is a representation of polymorphism in a class and is a compile-time polymorphism. Method overloading is a means for a class to handle different types of data in a uniform manner. Overloads occur in the same class. A method of the same name is considered overloaded if it has a different parameter list (number of arguments, type).
    • The overloading of Java methods is that you can create multiple methods in a class that have the same name but have different parameter lists. When invoking a method, it is the embodiment of polymorphism to decide which method to use by passing the number of arguments and the different types of parameters.
    • The type of the return value can be the same or different when overloaded, and cannot be distinguished as an overloaded function by the type of the return value.
    • cannot be overloaded by access rights.

Override (override, overwrite)

    • The polymorphism between the subclass and the parent class, the method of the parent class is redefined, is the diversity of the runtime. If a method defined in a subclass has the same name and parameters as its parent class, we say that the method is an overridden method. In Java, subclasses can integrate the methods of the parent class without having to rewrite the same method. But sometimes subclasses do not want to integrate the parent class's methods as they are, but want to make some modifications, which requires a method rewrite. Method overrides are also known as method overrides.
    • If a method in a subclass has the same method name, argument list, and compatible return type as a method in the parent class, the new method overwrites the original method. You can use the Super keyword, which refers to the parent class of the current class, if you want a method in the parent class.
    • The access adornment permission for a subclass method cannot be less than the parent class.

To put it another way, the method of the parent class must be either a virtual method or an abstract method when overridden in C #.

    • Override (Overwrite) the rule:
    • The parameter list of an overriding method must be exactly the same as the overridden method, otherwise it cannot be called overridden instead of overloaded.
    • The access modifier for the overridden method must be greater than the access modifier (public>protected>default>private) of the overridden method
    • The return value of the overridden method must be consistent or compatible with the return of the overridden method
    • The overridden method throws an exception that must match the exception thrown by the overridden method, or its subclass
    • The overridden method cannot be private, otherwise only a new method is defined in its subclass and is not overridden.
    • Static methods cannot be overridden as non-static methods (compilation errors are compiled).
    • When the parent class method is final, the subclass cannot override the method, regardless of whether the method is modified by public, protected, or by default.

Rules for overloading:

    • Overloading can only be implemented with the same method name and different parameter forms. Different parameter types can be different parameter types, different parameter numbers, different parameter order (parameter types must not be the same).
    • cannot be overloaded by Access permission, return type, thrown exception.
    • The exception type and number of methods do not affect overloading.

Contact and difference:
The overloads and overrides of a method are implemented in a polymorphic way, except that the former implements the polymorphism at compile time, while the latter implements the runtime polymorphism. Overloads occur in a class, and methods with the same name are considered overloaded if they have different parameter lists (different parameter types, different number of arguments, or both); Overrides occur between subclasses and parent classes, and overrides require subclasses to be overridden by methods that have the same argument list as the parent class, which has a compatible return type, Better access than the parent class is overridden by the method and cannot declare more exceptions than the parent class is overridden (the Richter substitution principle). Overloads do not have special requirements for return types and cannot be differentiated by return types.

Hide: When a parent class or subclass has a property or method with the same name, the property or method form of the parent class is missing, and the actual is still there.

    • When a hidden event occurs, the declared type is a class that invokes the property or method of the corresponding class without dynamic binding.
    • Method hiding has only one form, that is, the parent class and subclass have the same static method
    • Properties can only be hidden and cannot be overwritten.
    • class instance variable/static variable can hide instance/static variable of parent class.

The difference between hiding and overwriting:

    • Properties that are hidden are accessed by attributes in the parent class after the child class is cast to the parent class.
    • The overridden method that is called by the subclass itself after the subclass is cast to the parent class.
    • Because overrides are dynamic bindings, which are constrained by the RTTI (run time type identification, runtime types check), hidden from rtti constraints, summarized as Rtti only for overrides and not for shadowing.

Overload (overloaded) and override (override, overwrite) in Java

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.