Java-override of member variables and member functions

Source: Internet
Author: User

Java-override of member variables and member functions

The implementation of polymorphism in java is completed through class inheritance or interface implementation.

In the process of class inheritance or interface implementation, member attributes and member functions are overwritten. Note that the rewriting of member functions directly overwrites the parent class (in inheritance ), however, rewriting of member variables will not overwrite them.

For example:

Two classes: Person and Man;

Both have the property age with the same name and the method "say ()" with the same name ();

Public class Person {

Public int age;

Public void say (){

System. out. println ("person say ...");

}

}

    

Public class Man {

Public int age;

Public void say (){

System. out. println ("man say ...");

}

}

// Test

Public class Proc {

Public static void main (String [] args ){

Man man = new Man ();

System. out. println ("---------- attribute ----------");

Man. age = 20;

System. out. println (man. age); // 20

Person person = man;

Person. age = 25;

System. out. println (person. age); // 25

     

System. out. println ("---------- function ----------");

Man m = new Man ();

Man. say (); // man say...

Person p = m;

P. say (); // man say...

}

}

 

It can be seen from the above column that the member function can be rewritten. When a subclass method is called through the parent class, the override method is called, and the member attribute cannot be overwritten (even if it is overwritten, );

    

This distinction can be understood as follows:

From the storage point of view, attributes are stored in the heap, and the method is saved in the stack. If the method is not saved for a long time, the attributes are released after the execution. Therefore, the attributes remain unchanged (simple understanding, in details );

 

 

The above content is based on my personal understanding. If there are similarities, they are purely accidental. If you do not like them, please correct them. ---- 2016/7/17

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.