Questions about how to call a method after subclass inherits the parent class

Source: Internet
Author: User

1. Read the following section.Code

Class parentclass {Public String MSG = "I Am a attribute in parentclass. "; Public String getmsg () {return MSG;} class childrenclass extends parentclass {Public String MSG =" I Am a attribute in childrenclass. ";}public class testclass {public static void main (string [] ARGs) {parentclass P = new parentclass (); childrenclass c = new childrenclass (); system. out. println (P. getmsg (); system. out. PRI Ntln (""); system. Out. println (C. getmsg (); // The method in the parent class is not overwritten or called. }}

The output result is:

I am a attribute in parentclass.

I am a attribute in parentclass.

When the subclass does not override the method in the parent class, it calls the method in the parent class. when calling the method, it always finds out whether there is a subclass extension method, if not, go to the parent class. Here, the subclass does not have a method to extend the parent class.

2. dynamic binding

When a subclass object is created (new subclass (), the reference of the parent class is given on the left, that is, the parent class reference name = new subclass ()

The system dynamically binds the actual type of reference.

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.