Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java polymorphic methods inside constructors complete analysis     - UNIX how to restrict users by IP Telnet (Linux)

- CentOS7 yum install third-party source EPEL (Linux)

- Use Observium to monitor your network and servers (Server)

- Oracle 11g + RAC + RHEL6.5 + udev + ASM + PSU installation summary (Database)

- Redis Linux system installation guide (Database)

- RedHat Linux 6 desktop installation (Linux)

- The Samba service does not have permission to access (Server)

- shell script error dirname: invalid option - b (Database)

- How to install and configure in Ubuntu 14.10 'Weather Information Indicator' (Linux)

- Install the Red Hat Container Development Kit on OSX (Server)

- Android use canvas board painting (Programming)

- Bash job control (Linux)

- The temporary operation of the document mkstemp on Linux (Linux)

- Hadoop connection failed or stuck processing (Server)

- Linux GCC 5.1.0 compiler installation (Linux)

- Detailed installation OpenCV2.3.1 under CentOS 6.5 (Linux)

- Python developer of time-saving method (Programming)

- Handle large data problems Bit-map method (Programming)

- C ++ constant definition (Programming)

- Tmux Getting Start (Linux)

  Java polymorphic methods inside constructors complete analysis
  Add Date : 2016-11-15      
  Let's look at an example, if you read "Java programming ideas", then there should be the impression.

package com.test.zj;

public class PolyConstructors {

    public static void main (String [] args) {
        // TODO Auto-generated method stub
        new RoundGlyph (5);


class RoundGlyph extends Glyph {
    private int radius = 1;

    public RoundGlyph (int r) {
        // TODO Auto-generated constructor stub
        radius = r;
        System.out.println ( "RoundGlyph radius ==" + radius);

    void draw () {
        // TODO Auto-generated method stub
        System.out.println ( "RoundGlyph draw () radius ==" + radius);


class Glyph {
    void draw () {
        System.out.println ( "print glyph.draw ()");

    Glyph () {
        System.out.println ( "Glyph () before draw ()");
        draw ();
        System.out.println ( "Glyph () after draw ()");



For students who are generally based java Here you might think that the output is as follows:

1 Glyph () before draw ()
2 RoundGlyph draw () radius == 1
3 Glyph () after draw ()
4 RoundGlyph radius == 5

But in fact, after you finished running, you will find that his output is this:

Some people may read here still do not understand what I want to express, I write a simple example. Define a parent SuperClass

package com.test.zj;

public class SuperClass
    private int superValue;

    public SuperClass ()
        setSuperValue (100);


    public void setSuperValue (int x)
        superValue = x;


Then we define a subclass of it:

// The subclass inherits from the parent class superclass
public class SubClass extends SuperClass
    private int subValue = 10;

    public SubClass ()

    // This method overrides the parent class
    public void setSuperValue (int x)
        // First call the parent class method
        super.setSuperValue (x);
        // Then we assign a value to its own variables
        subValue = x;


    public void printSubValue ()
        System.out.println ( "subclass subvalue ==" + subValue);


Finally, write a main function can be

package com.test.zj;

public class MainClass {

    public static void main (String [] args) {
        // TODO Auto-generated method stub
        SubClass sc = new SubClass ();
        sc.printSubValue ();


Well, now I believe many people will think that the results of the second example of the output should be 100

But in fact there is nothing eggs with his actual results are:

Both of these examples that in the end what happened, we look at the direct bytecode Well, this certainly will not be wrong bytecode, jvm bytecode how to write on how to perform.

We take a look at the first example.

It should be obvious to see our main function is to start a new RoundGlyph this object. Then we look at the results this class -c bars

We can see the constructor of the class

The first implementation is this:

That is the first implementation of the assignment constructor glyph then When the constructor glyph completed before execution

Our radius as an int variable before being executed automatically initialized jvm his is 0!

So here you should be able to guess a vague about the constructor glyph executing first, and then give yourself a member variable radius assignment.

Then we look at the glyph have done it:

You see constructor glyph in the middle of the time 13: invokevirtual # 31 here, to execute the draw method, but subclasses we rewrite this draw method

So you see the draw for subclasses to call the constructor in the glyph subclass radius when the assignment has not been executed, so this method subclass

The output value is zero, of course!

When the constructor of the parent class glyph after the execution of our assignments subclass finally be implemented. So here you should be able to understand the first example.

That we can now look to the second example, we are in fact very much the same. We still look at the second example manclass and main functions

Look here first new main function of a subclass of the subclass of it. Then of course we will take a look subclass init method

In fact, this place is a Subclass constructor.

Here you can see very clearly superclass constructor in the constructor subclass of where we are first performed, and then assigned to their subValue 10.

Then we went to see what has been done in the superclass. But in fact we have been able to come here think no matter what you do in the superclass when you finish later

SubValue values are bound to 10.

So when you subclass after object construction is completed at this time his subvalue member variable value is 10, so of course you print out the value of this variable is necessarily 10 a.

Of course, for a little clearer I put superclass constructor little talk about what has been done, although there is what has been done will not affect our conclusions, but still talk about it,

Even if it does not use any eggs. . .

You see this is a moment to call this method setSuperValue it, right, because subclasses override this method so we certainly have to look at the sub-category

This method of doing it:

Can not you see that they call setSupervalue parent class do, then subsequent calls to see you have a iload putfield

These two operations are not that give us a subclass subvalue assignment for you, right. Been here, our object constructor subclass first step:

The constructor calls the parent class even completed, and finally completed after the implementation of his assignment:

Well, these two examples here, even if the analysis is completed.

In fact, the final conclusion is that java programming ideas in to say:

Parent class static member -> subclass static member -> ordinary members of the parent class initialization and initialization block -> parent class constructor -> subclass ordinary member initialization and initialization block -> subclass constructors

If you are interested, you can write a little bit more complex procedures, verify the above conclusion is established, nonsense. . . . This conclusion is certainly valid. but

If you use this command to see them javap -c bytecode deeper, then I believe you can understand it!

Finally, say one more thing, we usually write code, try to avoid such wording above two examples, because this situation caused by the bug difficult to find. . . which is:

Try not to operate subclass member variables in the constructor of the parent class inside. If we want to initialize write very much trouble, consider using the initialization block such a clear way!

Do not ask me why this research, because tmd there is a bug is discovered for a long time for this reason ah! So after you find someone so written, please write direct mail copy to the whole group of complaints.
- How to avoid two Chrome icon appears in ELementary OS Freya (Linux)
- Linux System Getting Started Learning: DeVeDe installed on Linux to create a video DVD (Linux)
- Manual cleaning Oracle audit records (Database)
- CentOS7 virtual machine creation failed Solution (Linux)
- Wireless LAN security solutions (Linux)
- 25 Git Usage Tips (Linux)
- Install MATE desktop environment adjustment tools Mate Tweak 3.3.6 (Linux)
- Golang environment configuration recommendations (Linux)
- By creating a personal technology blog Detailed GitHub (Linux)
- Ubuntu resolve sudo: source: command not found error (Linux)
- Nginx + ownCloud + PHP + MySQL to build personal private cloud under CentOS7 (Server)
- Based LNMP + uWSGI deploy Django applications (Server)
- Solve the compatibility problem between Linux and Java at the source in bold font (Linux)
- Linux file compression and archiving (Linux)
- Linux Platform Oracle 11g Single Instance Installation Deployment Configuration Quick Reference (Database)
- DRBD + Heartbeat solve NFS single point of failure (Server)
- Diagnose and resolve the SSH connection slow (Linux)
- Pydev installed and configured on the Eclipse (Linux)
- Virtualbox virtual machine can not copy CentOS Network (Linux)
- MySQL in order by inaccurate results in problems and solutions (Database)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.