DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Constructor Chaining in Java

Constructor Chaining in Java

Invoking constructors from one another can be frustrating. This guide will walk you through successfully chaining constructors while avoiding compiler errors.

Vineet Sharma user avatar by
Vineet Sharma
·
Mar. 31, 17 · Tutorial
Like (20)
Save
Tweet
Share
21.77K Views

Join the DZone community and get the full member experience.

Join For Free

Constructors are one of the simplest, yet trickiest object creation concepts in Java. A lot of experienced Java developers get confused about it. A source of confusion is relating it to methods, which is a totally different concept. One of the key differences between a method and a constructor is that constructors don't return anything. A lot of things happen under the hood when you invoke a constructor.

How Does Constructor Chaining Work?

When you invoke a constructor from another constructor, it's called constructor chaining. Whether you're dealing with a standalone constructor or a parent class' constructor, the end of the chain will always be the Object's class constructor because every class is inherited from the Object class by default.

So lets' look at the below scenario to understand it better:

class Parent {

    public void Parent() {
        System.out.println("inside parent without arguments");
    }

    public Parent(int a) {
        System.out.println("inside parent with argument");
    }
}

class Child extends Parent {

    public void Child() {
        System.out.println("inside child without arguments");
    }

    public Child(int b) {
        System.out.println("inside child with argument");
    }

    public static void main(String[] args) {
        Child child = new Child();
        Child child1 = new Child(15);
    }
}


So what should the output be? It will give a compilation error. 

1. Line 18 will give a compilation error saying that there is no default constructor in the Parent class. Huh?

Doesn't the compiler add a default constructor by itself? No.

Let's understand what the compiler is trying to do here: 

  • The compiler doesn't add a default constructor (no args) if you have already defined a constructor with arguments. 

  • The compiler tries to add a super() call as the first call in the constructor if you don't add it yourself.

2. Line 23 will give compiler error because there is no default constructor defined in Child.

So how can we fix it?

1. We can fix line 18 in the following two ways:

  • Add a super(b), super call with an argument to the Parent class in the constructor of the Child class.

  • Add a default constructor in the Parent class.

2. We can fix line 23 by adding a default constructor in the Child class:

class Parent {

    // this is method not a constructor
    public void Parent() {
        System.out.println("inside parent without arguments");
    }

    // this is default constructor.
    public Parent() {
        super(); // hidden call to Object's default constructor
        System.out.println("inside parent's default constructor");
    }

    public Parent(int a) {
        super(); // hidden call to Object's default constructor
        System.out.println("inside parent's argument constructor");
    }
}

class Child extends Parent {
    // this is a method
    public void Child() {
        System.out.println("inside child without arguments");
    }

    // this is a default constructor
    public Child() {
        super(); // hidden call to Parent's default constructor
        System.out.println("inside child's default constructor");
    }

    public Child(int b) {
        super(b); // Case - 1 : call to Parent's argument constructor
        System.out.println("inside child's argument constructor");
    }

    public static void main(String[] args) {
        Child child = new Child();
        Child child1 = new Child(15);
    }
}


Output : 

inside parents's default constructor

inside child's default constructor

inside parent's argument constructor

inside child's argument constructor


What Is this() Used For?

If you need to call the same class' constructor, then you can use this() as the first statement of the constructor. It can be with or without arguments based on which constructor you want to invoke.

class Child extends Parent {
    // this is a method
    public void Child() {
        System.out.println("inside child without arguments");
    }

    // this is a default constructor
    public Child() {
        this(); //can we call this here?
        System.out.println("inside child's default constructor");
    }

    public Child(int b) {
        this(); // invoke default constructor of Child class
        System.out.println("inside child's argument constructor");
    }

    public static void main(String[] args) {
        Child child = new Child(15);
    }
}


Output: 

Line 9, will give a compilation error. It is a recursive call to the default constructor. However, it is legitimate to call it on line 14 because it is being called from an argument constructor. If we remove line 9, then the output will be :


inside parent's default constructor 
(the compiler will add a call to super() in the default constructor of the Child class, which will invoke Parent's default constructor)

inside child's default constructor

inside child's argument constructor


I hope the concept of constructor chaining is clear now!

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 12 Biggest Android App Development Trends in 2023
  • API Design Patterns Review
  • Why You Should Automate Code Reviews
  • Top 5 PHP REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: