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