Java overload (Overload) and rewrite (override) and examples _java

Source: Internet
Author: User
Tags inheritance

A lot of students for overload and override silly points, suggest not to rote conceptual knowledge, to understand to remember.

First give me the definition:

overload (Overload): A group of methods that have the same name and different parameters in the same class or in a class that has an inheritance relationship. Nature is the salutation to different methods.

Override (Overwrite): Between two classes that have inheritance relationships, the methods existing in the parent class are redefined in subclasses. The essence is to give different implementations for the same method.

Let's look at the examples of overloading first:

public class overloadparent{Public

  string Getperson (string name) {return

    "persona" + name;

  }

  

  Public String Getperson (int.) {return

    ' personb ';

  }

  

  public string Getperson (String Name,int age) {return

    "Personc";

  }

  

  public void Getperson (String name) {

    System.out.println ("Am I an overloaded method?") ");

  }

}

public class Overloadchildextends Overloadparent {public

  void Getperson (double) {

     Sytem.out.println (" Am I the overloaded method ");

  }



Attention to observation:

(1) The same method in the Overloadparent with 4 names

(2) The first three methods have inconsistent parameter types and numbers, and the return values are consistent, which constitute an overload

(3) Method 4 and Method 1 only return the value is not the same, does not constitute an overload, the compiler does not pass.

PS: The return value is the result of the method execution, and we do not specify that "I want to call the return value is the xxx type method" When invoking the method, it does not become the characteristic of the method overload.

(4) Overloadparent inherited the method owned by Demo,demo, it has. It felt that the existing method could not meet the demand, simply overloaded one.

Overloaded Flags: The method name is the same, the parameter is different (number or type), regardless of the return value.

Let's look at one more-written example:

  public class overrideparent{public

     void Fly () {

       System.out.println ("Ican fly!");

    }

  

 

  public class Overridechild extends overrideparent{

    @override public

    void Fly () {

         System.out.println (" Ican ' t fly, but I can run! ");

    }

   public static vid main (string[] args) {

         overwriteparent child= new Overwritechild ();

         Child.fly ();

    }

  

What does the main method of executing overridechild output?

The answer is: I can ' tfly, but I can run!

We see:

(1) Overridechild and Overrideparent both have a fly method

(2) Fly return values and modifiers are the same, only the method body is not the same

(3) There is a @overwrite annotation before the fly method of subclasses, jdk1.5 appears, only for class inheritance, and 1.6 for interface implementations. This annotation helps the compiler to check, not to add.

Overridden Flags: Child-parent classes that have different implementations for the same method.

Application Scenarios

overloading: When methods function Similarly, different parameters need to be passed.

Overwrite: Subclasses have their own unique behavior, inherited from the parent class and cannot meet their own needs.

PS: Overload and overwrite are polymorphic expressions. The former is compiler polymorphism, the latter is run-time polymorphism.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.