<EffectiveJava> Reading notes--01 The use of inheritance note

Source: Internet
Author: User

1, methods in the constructor method of the parent class cannot be called to override the quilt class.

Analysis:

When initializing a subclass, the parent class is initialized first, that is, the constructor method of the parent class is called;

If other methods that can be overridden are called in the constructor of the parent class, then the call is actually the overridden method in that subclass;

So the subclass is not initialized, so it can cause some problems.

The sample code is as follows:

Parent class:

 Public class Father1 {    public  Father1 () {        overRide1 ();    }      Public void OverRide1 () {        System.out.println ("Member method of the parent class ...)     ........................

Sub-class:

 Public classSon1extendsFather1 {Private FinalDate date;  PublicSon1 () {Date=NewDate (); } @Override Public voidOverRide1 () {System.out.println (date); }     Public Static voidMain (string[] args) {Son1 son1=NewSon1 ();    Son1.override1 (); }}

If you run the main method of the subclass, the result of the console output is:

Null
Mon APR 12:55:48 CST 2017

When the parent class is initialized, the OverRide1 method of the subclass is called in the constructor method;

<EffectiveJava> Reading notes--01 The use of inheritance note

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.