Java Foundation--super Keywords

Source: Internet
Author: User

Super generally appears in the construction method of subclasses.
The super statement must be written in the first line, and super is typically used to invoke the constructor of the parent class.
is to initialize the properties of the parent class so that the subclasses can use these properties.

After the subclass inherits, if you do not define your own properties or override your own methods, then when you create an object, the object calls the properties and methods in the parent class.

If you have one, use your own properties and methods.

Use super in a Java class to refer to the composition of the parent class, use this to refer to the current object , and if a class inherits from another class, when we new the instance object of the subclass (when invoking the constructor)
This subclass object will also create a parent class object super. How do you refer to the parent object inside? Using Super to refer to, this refers to a reference to the current object, and super is a reference to the parent object inside the current object. (Quoted from http://www.cnblogs.com/xdp-gacl/p/3635948.html)

The Super object inside each subclass's constructor has one copy of the properties and methods of the parent class.

1  Public classTest {2      Public Static voidMain (string[] args) {3b b =NewB ();//Output Ten4B B1 =NewB (11);//Output 05     }6 }7 8 classA {9     intAge ;Ten } One classBextendsA { A     intAge//if the subclass does not define its own age attribute, then this.age points to the age that the parent inherits from, then the output will be ten . -      PublicB () { -         Super. age = 10;//assign the age of the parent class to a value of ten theSystem.out.println (Super. age); -     } -      -      PublicBintAge ) { +          This. Age = Age;//Age of the child class -System.out.println (Super. age);//The above construct is assigned, but this construct is not assigned to a value +     }         A}

(Study records, only for reference, follow-up will continue to amend the supplement.)

Java Foundation--super Keywords

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.