Summary of java Method for Establishing Subclasses

  • 2021-07-26 07:36:21
  • OfStack

Java In the constructor line 1 initializes by calling the constructor in the parent class. After the parent class is initialized, the attributes of the subclass are displayed and initialized. Specific initialization of subclass constructors. After initialization, the address value is assigned to the reference variable Person p. An example of creating a subclass is included with this article.

Person p = new Person();

1. JVM will read the Person. class file under the specified path and load it into memory, and will load the parent class of Person first (if there is a direct parent class).
2. Open up space in heap memory and allocate addresses.
3, and in the object space, initialize the properties in the object by default.
4. Call the corresponding constructor for initialization.
5. In the constructor, Line 1 calls the constructor in the parent class for initialization.
6. After the parent class is initialized, the attributes of the subclass are displayed and initialized.
7. Specific initialization of subclass constructors.
8. After initialization, assign the address value to the reference variable Person p.

Example:


class Fu

{

  Fu()

  {

    super();

    show(); // It can be seen from the results that the child parent class has the same name show Method, calling a subclass show Method. 

    return;

  }

 

  void show()

  {

    System.out.println("fu show");

  }

}

class Zi extends Fu

{

  int num = 8;

  Zi()

  {

    super();

    //--> Pass super When initializing the contents of the parent class, the member variables of the subclass do not show initialization. Etc super() After the parent class is initialized, 

    // The member variable display of the subclass is initialized. 

 

    System.out.println("zi cons run...."+num);

    return;

  }

  void show() 

  {

    System.out.println("zi show..."+num);

  }

}

class ExtendsDemo5 

{

  public static void main(String[] args) 

  {

    Zi z = new Zi(); 

    z.show();

  }

}


Related articles: