A polymorphic method inside the Java constructor

Source: Internet
Author: User

This article mainly discusses the Java constructor inside the polymorphic method, more Java technical knowledge, please visit the Crazy Software Education website.

We know that the invocation of a dynamic binding is determined at run time, and the object cannot know exactly which class to call the method.

When we invoke the dynamic binding method in the constructor, we use the definition after the method is overwritten. However, the effect of this invocation is difficult to predict because the overridden method is called before the object is fully constructed. Let's take a look at the following code:

Java code

Class base{

Private String name = "Base";

Base () {

Tellname ();

Printname (name);

}

public void Tellname () {

System.out.println ("Tellfather" + name);

}

public void Printname (String name) {

System.out.println ("Printfather" + name);

}

}

Class DriveD extends base{

Private String name = "DriveD";

DriveD () {

Tellname ();

Printname (name);

}

public void Tellname () {

System.out.println ("Tellchild" + name);

}

public void Printname (String name) {

System.out.println ("Printchild" + name);

}

}

public class Testobject {

public static void Main (String args[]) {

New DriveD ();

}

}

As you can see from the code, subclasses derived methods that override the parent class base. When we instantiate a derived object, the constructor of the parent class is called automatically, but the dynamic binding method is called in the parent class constructor

Java code

Tellname ();

In this way, the Tellchild () method of the subclass is called in the parent class constructor. At this point, the parent class base has completed initialization: Because the subclass derived has not completed initialization at this time, its instance variable name is null by default. Then the parent class constructor exits, goes back to the subclass constructor call, and then completes the subclass initialization, and then the subclass constructor call is complete.

The results of the code run as follows;

Java code

Tellchild NULL

Printchild Base

Tellchild drived

Printchild drived

The actual initialization process should look like this:

1. Before anything else happens, initialize the storage space allocated to the object to binary 0.

2. In general, the parent class constructor is called, and the method overridden by the quilt class is called in the parent class constructor (if there is a method called dynamic binding in the constructor).

3. Invoke the initialization method of the member in the order in which they are declared.

4. Call the constructor of the subclass.

As can be seen from the above example, the method that can be safely called in the constructor cannot be inherited, that is, the method is final decorated, or the method is private (the private method automatically belongs to the final, hehe).

Guangzhou Crazy software Java training keep up with the latest Java Power Flow technology, the first in the country to explain the new Java 8 features, according to the needs of enterprise work in the development of technology, so that students learn the curriculum is the most needed practical technology, but also to ensure that students can learn these skills points, learn, and crazy software education courses in real-time update, in the rapid development of information technology, can ensure that students learn to update more mainstream technology. Teachers are strong, the teacher has more than 8 years of project experience, Crazy software Java course is the combination of theory and practice, so as to ensure that the students have a good grasp of theoretical knowledge at the same time, can also be based on theoretical guidance to do practical work. In order to cultivate students ' practical ability, many practical courses have been set up, in which students can experience the real process of actual project, so as to master knowledge more deeply. To learn more, you can visit the Crazy Software education website to inquire.

A polymorphic method inside the Java constructor

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.