6 hands-on brain and classroom examples

Source: Internet
Author: User

1. Hands-on Brain: Construction method invocation under inheritance conditions (testinherits.java)

1)Super(parameter): Call one of the constructors in the base class (should be the first statement in the constructor) , super: it refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class) , the base class is defined with the same member in the derived class as:super. Variable name Super. The member function is the reference name (argument). Super () calls the constructor method of the parent class from the subclass . The call to super () must be written on the first line of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time. Super () needs to be placed in the first row within the construction method.

2) Why do I have to call the constructor method of the parent class before running the constructor of the subclass? Can you turn around? Why can't it be reversed?

No, I can't. The subclass inherits the various attributes of the parent class, and the constructor method is equivalent to instantiating the parent class, and if you do not invoke the constructor of the parent class when you instantiate the child class, the subclass will not inherit without the father.

3)

2, classroom example:Explorationjdksource.java

1) A class with no member variables is created in the program, and the class is allocated memory and output address on the stack.

2)

3, classroom example:Fruit.java

1) in the "+" Operation , when any object is connected to a String object, it is implicitly called by its ToString () method, by default, this method returns "class name @ + hashcode". To return meaningful information, subclasses can override the toString () method

2)

4, classroom example:Address.java

1) The so-called immutable class means that when an instance of this class is created, it is not allowed to modify its property values. in the JDK 's base Class library, all basic types of wrapper classes, such as Integer and Long , are immutable classes. Java.lang.String is also an immutable class , Although he is not a basic type.

2)

6 hands-on brain and classroom examples

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.