java-overloading, rewriting (cool analysis)

Source: Internet
Author: User

I. Overloading of methods

Overloading is to automatically match the corresponding method according to the different data types. The overload determines which method to invoke when compiling, not the same as the rewrite. (Common, constructor overloading).

Give me a chestnut:

1. Overloads of the basic data type:

1 /**2  * @authorSchrödinger's Cat3 * Overloads of the base data type4  * */5  Public classMain {6     7     Static voidMethed (byteb) {8System.out.println ("Byte:method");9     }Ten      One     Static voidMethodDoubled) { ASystem.out.println ("Double:method"); -     } -      the     Static voidMethodinti) { -System.out.println ("Int:method"); -     } -      +     Static voidmethod (String s) { -System.out.println ("String:method"); +     } A      at      Public Static voidMain (string[] args) { -Methed ((byte) 1); -Method (2.0); -Method (1); -Method ("Zpoor"); -     } in } -  to /* + * Results: - * Byte:method the * Double:method * * Int:method $ * String:methodPanax Notoginseng */

2. Overloading with Object reference parameters

1 /**2  * @authorSchrödinger's Cat3 * Overloads with object reference parameters*/4 classAnimal {5     6 }7 8 classPigextendstest{9     Ten } One  A classTest { -     Static voidmethod (Test a) { -System.out.println ("Animal"); the     } -      -     Static voidmethod (Pig p) { -System.out.println ("Pig"); +     } -      +      Public Static voidMain (string[] args) { ATest A =NewTest (); atPig p =NewPig (); -Test B =NewPig (); -          - method (a); - method (p); -Method (b);//why not pig, but animal in         /* - * Blind Analysis: Look at the reference type rather than the object type, the overload is determined at compile time, to * The reference type determines which overloaded method.  +          * */ -          the     } * } $ Panax Notoginseng /** - * Results: the * Animal + Pig A Animal the */

Summary: Overloaded rules

1, the overloaded method must change the parameter list, the parameters must be different (the number of parameters, the type of parameters, the order of the parameters)

2, the overloaded method is not related to the return value, not by the return value type to determine whether the method is overloaded.

3. Overloaded methods can change the access modifier.

4. Overloaded methods can declare new or more extensive plant anomalies.

5, the method can be overloaded in a class or a subclass.

Second, the method of rewriting

Overrides can only appear in an inheritance relationship, as long as the parent class's methods are not final decorated, and the parent class's methods are overridden. A bit of rewriting: the ability to define characteristics specific to a subclass.

Give me a chestnut:

1 /**2  * @authorSchrödinger's Cat3 * Overloading Example*/4 classAnimal {5      Public voideat () {6System.out.println ("Eat Something");7     } 8 }9 Ten classHorseextendsAnimal { One      Public voideat () { ASystem.out.println ("Eat some Grass");//Implementing Overloading -         } -}

For an abstract method inherited by a parent class, either the subclass overrides the method or the handle class is also set as an abstract subclass. So the abstract method can be said to be a method that must be rewritten.

The meaning of the rewrite:

Overrides can implement polymorphism by pointing to the subclass object with a reference to the parent class.

Give me a chestnut:

1 /**2  * @authorSchrödinger's Cat3 * Rewrite example*/4  Public classTest {5      Public Static voidMain (string[] args) {6Animal p =NewPig ();7 p.eat ();8     }9 }Ten  One  A classAnimal { -      Public voideat () { -System.out.println ("Eat something."); the     } - } -  - classPigextendsAnimal { +      Public voideat () { -System.out.println ("Eat some hogwash beautiful"); +     } A}

Overloading principle: (Blind analysis):

What references are used, the compiler invokes only the methods owned by the reference class. If you call a subclass-specific method, you will get an error. In other words, the compiler only looks at the reference; Type, not the object type.

Rules for overriding methods:

1. The overriding method cannot have a more restricted access limit than the overridden method. (but more broadly, the parent class is package access, and the subclass's overriding method allows public access, paying special attention to tosting ())

2. The parameter list must be the same as the overridden method (the difference between overriding and overloading)

3. The overriding return type must be the same as the return type of the overridden method

4. The override method cannot throw a new exception, or a check exception that is wider than the check exception declared by the overloaded method. But you can throw fewer, more limited, or don't throw exceptions

5. The final modified method cannot be overridden

6, a method can not be inherited, you can not rewrite him (such as the final modified class method, there is the parent of the private decoration method)

Okay, blind analysis is over, write some code pressure yajing.

java-overloading, rewriting (cool analysis)

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.