Java constructor Internal Polymorphism method

Source: Internet
Author: User

/**
* Created by Administrator on 2017/3/7.
*/

/**
* @author Zhenghong
* @date 2017-03-07-21:21
**/
public class TESTC {
Public static void main (String []args)
{
New Graph (5);
}
}
Class GRP
{
void Draw ()
{
System.out.println ("Draw ....");
}
GRP ()
{
Draw ();
}
}
Class Graph extends GRP
{
private int radius = 2;
Graph (int radius) {
This.radius = radius;
}
@Override
void Draw () {
System.out.println ("Draw.." +radius);
}
}

Result Draw ... 0
Initializes the graph, initializes the parent class, has an overloaded method in the parent constructor method, and then calls the child class's draw () method, and prints the result as draw: 0, because the subclass radius has not been initialized at this time.
Thinking in Java Page 299
The guidelines for writing constructors use the simplest possible method of getting objects into normal state, and avoid invoking other methods if you can.
The only method that can be safely called in the constructor is the final method in the base class. (also applies to private methods, which automatically belong to the final method)

Java constructor Internal Polymorphism method

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.