Java overloads and Overrides _20150804

Source: Internet
Author: User

Java overloads and overrides _20150804

Overloading and rewriting are completely different syntax phenomena, the differences are as follows:

Overloading: A method that defines multiple methods with the same name but different parameter lists in a class, and determines which method to bind, depending on the number and type of arguments, at compile time.
Rewrite: Refers to a method defined in a subclass that is exactly the same as the parent class, and calls different methods depending on the type of the object (not the reference type) when the program is run.


Analyze the output of the following code:

Package Day04;class Super {public    void F () {        System.out.println ("Super.f ()");}    } Class Sub extends Super {public    void F () {        System.out.println ("Sub.f ()");}    } Class Goo {public    void g (Super obj) {        System.out.println ("G (Super)");        OBJ.F ();    }    public void g (Sub obj) {        System.out.println ("G (sub)");        OBJ.F ();    }} Class Test02 {public    static void Main (string[] args) {        Super obj = new Sub ();        Goo Goo = new Goo ();        GOO.G (obj);}    }



Analysis of the above code, the output is:

G (Super)

Sub.f ()

Analysis:

First, overloading follows the so-called " compile-time binding " that determines which method should be called at compile time based on the type of the parameter variable , because the variable obj is the Super class

So, Goo G (super) is called to Output G (super) first.

Overrides follow the so-called " run-time bindings " that invoke methods at run time, based on the type of the actual object that the reference variable points to , because obj actually points to

is an object of sub-class sub, so the subclass-overridden F method is called, Sub.f ().


Summary: Overloading and rewriting do not have a relationship, but there are a lot of differences, this small example is a comprehensive summary of the difference between overloading and rewriting and essentials. Master This small example, for overloading and rewriting the basic master!!!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java overloads and Overrides _20150804

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.