Inheritance and Interface class assignments

Source: Internet
Author: User

(1) Hands-on experiment: Construction method invocation under inherited conditions

Run the testinherits.java Sample, observe the output, and note that the call relationship that summarizes the constructor method between the parent class and the subclass modifies The code of the parent constructor method, explicitly calling Grandparent Another constructor, 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) What is the constructor of a subclass that must call the constructor of the parent class before it is run? Can you turn around? Why can't it be reversed?

Constructs an object that first calls its constructor method to initialize its member functions and member variables. Subclasses have member variables and member methods of the parent class, and if not called, the parent class inherits the correct initialization of member variables and member methods.

(3) Running Address.java

(4) See Explorationjdksource.java example

In this example, a class A is defined, and it does not have any members:

Class A {}

Example directly outputs the object created by this class

public static void Main (string[] args) {

System.out.println (New A ());

}

Operation Result:

(1) Use javap -C command to disassemble explorationjdksource.class;

(2) Read the bytecode instruction and figure out what the println () statement called.

public void println (Object x), which internally invokes the valueOf method of the String class .

The object.tostring method is also called inside the ValueOf method:

Public String toString () {

Return GetClass (). GetName () + "@" +

Integer.tohexstring (Hashcode ());

}

The Hashcode method is a local method that is implemented by the JVM Designer:

public native int hashcode ();

(5) Running Fruit.java

A: The fruit class overrides the ToString method of the object class, 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".

Inheritance and Interface class assignments

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.