Java override (Override) vs. overload (overload)

Source: Internet
Author: User
Tags throw exception

Overloading is the same method that can be processed differently depending on the input data, overriding the same method that the subclass inherits from the parent class, entering the data, but to make a response different from the parent class, you overwrite the parent class method

A concise understanding of overloading and rewriting:

    • overloading reflects the "improvise". The same function, depending on the type of data, takes different approaches. For example, the same is eating, to high-end restaurants to eat Western food, you will wear suits, but to eat hot pot, wearing casual clothes is more appropriate.
    • overrides reflect "parent-child differences." You "inherit" the father to eat hot pot hobby, but eat the same pot (note, the same data type), you like to rinse red soup, your father likes to rinse the broth.
Override (Override)

Overrides are subclasses that rewrite the implementation of the method that allows access to the parent class, and the return value and formal parameters cannot be changed. that is, the shell does not change, the core rewrite!

The benefit of overriding is that subclasses can define their own behavior as needed. That is, subclasses can implement methods of the parent class as needed.

The overriding method cannot throw a new check exception or a broader exception than the overridden method declaration. For example: A method of the parent class declares a check exception ioexception, but cannot throw Exception exception when overriding this method, because Exception is the parent of IOException, only the subclass exception of IOException can be thrown.

overriding rules for methods
    • The argument list must be exactly the same as the overridden method;
    • The return type must be exactly the same as the return type of the overridden method;
    • Access permissions cannot be lower than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected.
    • A member method of a parent class can only be overridden by its subclasses.
    • A method that is declared final cannot be overridden.
    • A method declared as static cannot be overridden, but can be declared again.
    • Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.
    • Subclasses and parent classes are not in the same package, the subclass can only override non-final methods that are declared public and protected by the parent class.
    • The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
    • The construction method cannot be overridden.
    • If you cannot inherit a method, you cannot override this method. Overloading (overload)

      Overloading (overloading) is a class in which the method name is the same and the parameters are different. The return type can be the same or different.

      Each overloaded method (or constructor) must have a unique list of parameter types.

      The most common place is the overload of the constructor.

      Overloading rules

      • The overloaded method must change the parameter list (the number of parameters or the type or order is different);
      • The overloaded method can change the return type;
      • Overloaded methods can change the access modifier;
      • Overloaded methods can declare new or broader check exceptions;
      • Methods can be overloaded in the same class or in a subclass.
      • The return value type cannot be used as a distinguishing criterion for overloaded functions.

Java override (Override) vs. overload (overload)

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.