JAVA inheritance: the relationship between compilation and running (on the left side of compilation, on the right side of execution), and java inheritance

Source: Internet
Author: User

JAVA inheritance: the relationship between compilation and running (on the left side of compilation, on the right side of execution), and java inheritance

"Member variables, static methods to the left; non-static methods: compilation to the left, running to the right ."

Meaning: when the parent class variable references a subclass object (Fu f = new Zi ();), in the object to which the referenced variable f points, the member variables and static methods are consistent with the parent class. Their non-static methods are consistent with the parent class during compilation, the runtime is the same as that of the subclass (where a secondary write occurs ).

Example:

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 the parent class

F. method4 (); // consistent with the parent class

F. method3 (); // consistent with the parent class during compilation, and consistent with the Child class during runtime

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 this sentence into two paragraphs: Fu f; this is to declare a variable f as the Fu class, then we know that f is definitely a Fu class. Then f = newZi (); creates a subclass object and assigns it to f. What is the result ??

The result is that the Fu class Object ---- f ------ is overwritten by the Zi class function ------.

(Shanghai Shang xuexiang java original)
That is to say:

Only the subclass function overwrites the Function Change of the parent class, but f must be the Fu class, that is to say, f cannot be changed to another class such as Zi (suddenly f has a special function of Zi class, and member variables are impossible ). So f Represents a Fu class after the function is rewritten (meaning of polymorphism), while the original member variables of the Fu class (not member functions cannot be rewritten) no changes ---------------- conclusion: A: member variables: Compile and run both depends on Fu.

However, f's Fu functions are rewritten.
Conclusion: B: non-static method: Compile to see Fu, run to see Zi

For static methods: Compile and run both fu !!
 

In fact, it is very simple. First, we need to understand what happens under static conditions?


When it is static, all functions of the Fu class are loaded with the Fu class. That is, a function of the Fu class (it exists before the object is created, and cannot be rewritten by the Zi class object that appears later. Therefore, if no re-write occurs, the following result is obtained: C: static Method: Compile and run both Fu

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.