"Java" interface and inheritance lesson after the hands of the brain

Source: Internet
Author: User

1. Run the Testinherits.java sample, observe the output, and note that the call relationship between the parent class and the subclass constructs the method, modifies the code of parent constructor method, and explicitly calls the other constructor of grandparent, notice whether the calling code is the first sentence, the impact is significant!

Conclusion: By super calling the base class construction method, it must be the first statement in the subclass construction method.

2. Thinking: Why must the constructor of the subclass be called before the constructor of the child class is run? Can you turn around? Why can't it be reversed?

The main function of a constructor is that a constructor is a special method of a class that is automatically called by the system when it is used to generate an instance, and cannot be called directly by the programmer. The constructor method name is the same as the same name and the argument is empty. The child class inherits the parent class by default, inheriting the constructor of the parent class: The subclass has an implicit method: Super (), and if the subclass overrides the constructor the subclass also implicitly calls Super ()

3. Discover the Mysteries of technology

See Explorationjdksource.java Example This example defines a Class A, which does not have any members: the Class A {} example directly outputs objects created by the classes public static void main (string[] args) {Sy Stem.out.println (New A ()); }

We got a strange running result: [email protected]

In the preceding example, the main method actually calls the public void println (Object x), which internally invokes the ValueOf method of the String class. The ValueOf method internally calls the Object.ToString method: Public String toString () {return getclass (). GetName () + "@" + integer.tohexstring ( Hashcode ()); The Hashcode method is a local method, implemented by the JVM Designer: public native int hashcode ();

4. Magical Plus

In the example on the previous page, the fruit class overrides the ToString method of the object class.

Conclusion: In the "+" operation, when any object is connected to a string object, its ToString () method is called implicitly, and by default, this method returns "class name @ + Hashcode". To return meaningful information, subclasses can override the ToString () method.

"Java" interface and inheritance lesson after the hands of the brain

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.