Java, when polymorphism encounters overload

Source: Internet
Author: User
Class cat {public void miao () {system. out. println ("I am a cat") ;}} class whitecat extends cat {public void miao () {system. out. println ("I Am a whitecat");} public class test {/*** @ Param ARGs */public static void getcat (CAT) {system. out. println ("cat ----"); cat. miao ();} public static void getcat (whitecat) {system. out. println ("whitecat ----"); whitecat. miao ();} public static void main (string [] ARGs) {// todo auto-generated method stubcat cat = new CAT (); CAT whitecat = new whitecat (); whitecat whitecat1 = new whitecat (); getcat (CAT); getcat (whitecat); getcat (whitecat1 );}}

The output is

Cat ----
I am a cat
Cat ----
I am a whitecat
Whitecat ----
I am a whitecat

--------------------------------------------------------
Conclusion: There are multiple overload methods, which can be selected during the call. If the parameters of different methods are inherited, the selection is determined during the compilation period, which is determined by the reference type, instead of Object Type

In the method body, calling the polymorphism method will return to "normal ".

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.