2 forms of representation method overloading and method overrides for Java polymorphism

Source: Internet
Author: User


Method Overloading: in the same class, the method name is the same, and the argument list differs by 2 or more methods that form the overloads of the Method.

Method Overrides: subclasses Re-implements the methods in the parent class.

1. Overloaded instances of methods (overload)

  We can define methods that have the same name and differentiate them by defining different input Parameters.

When called, the VM chooses the appropriate method to execute according to the different parameter Styles.

/*** method overload satisfies the condition * 1. In the same class, the method name is the same, and the parameter list differs by 2 or more methods that make up the overloaded * 2 of the Method. the parameter list refers to the type of the parameter, the number of parameters, and the order of the parameters at least one different * 3. The Method's modifier can be different. */ public classoverload{ public Static voidOverload (intI)    {System.out.println (i); }        /*the following methods are compared to the first method public static void overload (int I)*/    //public int overload (int k) {}//error, same argument list//return value type is not the same     public intOverload (intIintk) {        return0; }    //modifiers are different    Private voidOverload (DoubleD) {            }    //modifier, The return value type is different    Private DoubleOverload (DoubleDintk) {        return0; }}

2. Overlay instance of the method (override)

  overriding override means that a method in a subclass can be exactly the same as the name and argument of a method in the parent class.

When this method is called by an instance object created by a subclass, the definition method in the subclass is called, which is equivalent to the parent class

The exact same method defined in the overlay, which is also a representation of the polymorphism of object-oriented Programming.

/*** Method overlay refers to a subclass that re-implements the method in the parent class * Method overrides the Constraint: * 1. The method name of the subclass, the parameter list, the return value type must be the same as the name of the method in the parent class, the parameter list, and the return value type * 2. The subclass method cannot reduce access to the parent method, but A subclass method cannot throw more exceptions than a parent class method, and can not throw exceptions*/ public classoverride{}classException1extendsexception{}classException2extendsexception1{}classException3extendsexception2{}classparent{voidMethod ()throwsexception2{}}classSonextendsparent{//private void Method () {}//error, reducing access to the parent class method//int method () {}//error, method Return value is inconsistent with parent class method    voidMethodintK) {}//overwrite by compiling, but not with the parent class method ()//public void method () throws exception1{}//error throws more exceptions than parent class//public void method () {}//proper extension of access to the parent class method without throwing an exception//void method () throws exception3{};//a properly thrown exception is smaller than the exception thrown by the parent class     public voidMethod ()throwsexception3{}}

2 forms of representation method overloading and method overrides for Java polymorphism

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.