Java super keyword understanding

Source: Internet
Author: User

Example:

Person. Java
Bytes ------------------------------------------------------------------------------------------------
Package com. Android. xxx;

Public abstract class person {
Protected string name;

Public void setname (string Na ){
Name = Na;
}

Public abstract void display ();
}
Bytes ------------------------------------------------------------------------------------------------

Customer. Java
Bytes ------------------------------------------------------------------------------------------------
Package com. Android. xxx;

Public Class Customer extends person {
Private int testvar = 0;
Public void display (){
System. Out. println ("Customer:" + super. Name );
}
}
Bytes ------------------------------------------------------------------------------------------------

VIP. Java
Bytes ------------------------------------------------------------------------------------------------
Package com. Android. xxx;

Public class VIP extends customer {
Private string Tel;

Public VIP (string Na, string t ){
Super. setname (NA );
Tel = T;
}

Public void display (){
Super. Display ();
System. Out. println ("Tel:" + Tel );
}
}
Bytes ------------------------------------------------------------------------------------------------

Javatest. Java
Bytes ------------------------------------------------------------------------------------------------
/**
*
*/
Package com. Android. xxx;

/**
* @ Author XXXXXXXX
*
*/
Public class javatest {

/**
*
*/
Public javatest (){
// Todo auto-generated constructor stub
String name = "testname ";
String Tel = "1234567890 ";
Vip mvip = new VIP (name, TEL );

MVIP. Display ();
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Javatest mjavatest = new javatest ();

System. Out. Print ("++ 1 ");
System. Out. println ("++ 2 ");
System. Out. println ("++ 3 ");
System. Out. Print ("= END = ");
}
}
Bytes ------------------------------------------------------------------------------------------------

Output result:
Customer: testname
Tel: 1234567890
++ 1 ++ 2
++ 3
=== End ====

Analysis:
From the memory perspective, the above situation is very understandable.

From the memory perspective, only two objects, mjavatest and MVIP, are generated. mjavatest is not considered. The memory references corresponding to MVIP include:
Protected string name;
Private string Tel;
Public VIP (string Na, string T)
Public void setname (string Na) inherits from parent class
Inherited from the display () of the parent class ()
And your own display ()

When a javatest object is generated, a VIP object is generated through the constructor, which calls the constructor of the VIP.
The VIP constructor VIP () is called before and after the VIP instance method display ().
In VIP (), super. setname (NA); because the VIP subclass does not overwrite the setname of the parent class, the setname inherited from the parent class is called without super, set your own name to NA (not the name of the parent class or the parent class, because at this time the memory only has the VIP object instance ), here, we only add super to prevent the current interface of the parent class from being overwritten, resulting in the difference between the setname and the setname of the parent class. In fact, the protection member name of the object MVIP is set to Na without the use of super.

Display () calls super. Display (). Super is required here, because this class overwrites the display () of the parent class customer. The display of the parent class prints super. Name, which is equivalent:
MVIP's own:
Public void display (){
Super. Display ();
System. Out. println ("Tel:" + Tel );
}
To the following
Public void display (){
System. Out. println ("Customer:" + super. Name); // ------> here, super. Name is the name of the MVIP, inherited from the parent class. The VIP class does not redefine the name, so it does not hide the name inherited from the parent class. In this example, the super effect is the same as the name inherited from the parent class.
System. Out. println ("Tel:" + Tel );
}

I don't know if I understand it or not. Thank you for your comments.

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.