Java Construction methods

Source: Internet
Author: User
Tags throw exception

1. Customize the construction method, the default construction method will also be created, why?

If you customize the construction method, no hidden parameterless construction methods are provided.

Question: Why is Java not providing a hidden construction method after customizing the constructor method?

The function of the constructor is to initialize the member variable, and the user customizes the construction method, which is naturally like creating an object with a specific initial value, and if the implicit construction method is provided by default at this time,

Externally, you can create an object that does not conform to the author's expectations.

As an example:

            

public class Rect {    private float width;    private float height;    Public Rect (float width, float height) {        if (width <= 0 | | height <= 0) {            //parameter not justified throw exception            throw new Illega Largumentexception (                    String.Format ("Size must > 0. W:%f, H:%f ",                            width, height));        }        This.width = width;        this.height = height;    }}

 

2. Parent-Child class

Question 1: If the parent class defines a constructor with parameters, the subclass also defines the constructor with the parameter, which is an error

Implicit super constructor P () is undefined. Must explicitly invoke another constructor

This means that the parent constructor is not defined and must be explicitly a different constructor.

The reason is that when you create a subclass in Java, you need to create a parent class, and the subclass calls the parent class's hidden constituent method by default.

But when you customize the parent constructor method, the subclass cannot find the default constructor method. So you need to explicitly specify which parent constructor to call.

Two ways to solve:

1. Show write out parent class default constructor method

2. Explicitly call the parent class in the subclass constructor method which constructor such as super (name);

          

Package test; scenario 1:class p{static {System.out.println ("P Static");} {System.out.println ("P con");} Public p () {System.out.println ("Default P");} Public P (String name) {System.out.println ("with reference P");}} Class C extends P{static {System.out.println ("C Static");} {System.out.println ("C con");} Public C () {}public C (String name) {System.out.println ("with parameter C");}} public class Test5 {public static void main (string[] args) {c c = new C ("Zhang San");}}

  

Scenario 2:package Test;class p{static {System.out.println ("P Static");} {System.out.println ("P con");} Public P (String name) {System.out.println ("with reference P");}} Class C extends P{static {System.out.println ("C Static");} {System.out.println ("C con");} Public C () {super ("");} Public C (String name) {super (name); System.out.println ("With reference C");}} public class Test5 {public static void main (string[] args) {c c = new C ("Zhang San");}}

  

          

          

3. Create subclass Object Invocation Order

      

 Packagetest;classp{Static{System.out.println ("P Static");} {System.out.println ("P con"); }         PublicP (String name) {System.out.println ("With reference P"); }}classCextendsp{Static{System.out.println ("C Static");} {System.out.println ("C con"); }     PublicC () {Super(""); }     PublicC (String name) {Super(name); System.out.println ("With reference C"); }} Public classTEST5 { Public Static voidMain (string[] args) {c C=NewC ("Zhang San"); }}

Results:

P Static
C Static
P Con
With reference P
C Con
With reference C

So the order of execution is: parent static block-child static block-parent building block-parent constructor Method-child building block-child construction method

Note: Which problem is called by the parent constructor method:

If not specified then this is the default constructor method

If specified (as in this example, super (name)), the parameter constructor method that calls the parent class ...

    

Java Construction methods

Related Article

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.