Java inheritance: Compile-to-run relationships (compile to the left, run to the right)

Source: Internet
Author: User

"Member variable, static method see Left; non-static method: Compile look to the left and run to the right. ”

This means: When a parent class variable refers to a subclass object (Fu f = new Zi ()), in the object to which the reference variable F points, his member variables and static methods are consistent with the parent class, and his non-static method, at compile time, is consistent with the parent class, while the runtime is consistent with the subclass (replication occurs).

Cases:

Class Fu {

Intnum = 5;

static void Method4 () {

System.out.println ("Fu Method_4");

}

void Method3 () {

System.out.println ("Fu Method_3");

}

}

Class Zi extends Fu {

Intnum = 8;

static void Method4 () {

System.out.println ("Zi method_4");

}

void Method3 () {

System.out.println ("Zi method_3");

}

}

Class DuoTaiDemo4 {

public static void Main (string[] args) {

Fu f = new Zi ();

System.out.println (f.num);//consistent with parent class

F.METHOD4 ();//consistent with parent class

F.METHOD3 ();//compile-time consistent with parent class, runtime consistent with subclass

Ziz = new Zi ();

System.out.println (Z.num);

Z.METHOD4 ();

Z.method3 ();

}

}

Output Result:

5

Fu Method_4

Zi Method_3

8

Zi Method_4

Zi Method_3

Analysis:

Fu f = new Zi ();----------first understand what the variable f is, divide the sentence into 2 paragraphs: Fu F; This is the class that declares a variable F for FU, so know that F is definitely the Fu class. And then we F=newzi (); Set a subclass object to the F, what is the result??

As a result, you have the Fu class object covered by the Zi class function----f------。

----------------------------------------means:

Only the function of the subclass overrides the change of the function of the parent class, but F is certainly the class of Fu, which means that f is not likely to be a class such as Zi and so on (suddenly F has the Zi class-specific function, member variables, etc.). So f represents a Fu class where the function is replicated (polymorphic meaning), and the original member variable of the Fu class (not the member function cannot be replicated) has no change----------------get the conclusion: A: Member variable: Compile and Run all look fu.

But the FU class function of f has been replicated. --------------Get the conclusion: B: Non-static method: Compile look fu, run to see Zi

For static methods: Compile and run all look fu!!

In fact, it is very simple, first of all we have to understand what happens in the static situation?

----------------when static, all the functions of the Fu class are loaded with the FU class loaded. That is, the Fu class function (is before the object is established before the existence of the Zi class object can not be replicated, so there is no replication, then get: C: Static method: Compile and run See Fu

Java inheritance: Compile-to-run relationships (compile to the left, run to the right)

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.