Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3390574
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:42:27+00:00 2026-05-18T03:42:27+00:00

I hava slight doubt regarding the Output of the Program of Constructor Chaining I

  • 0

I hava slight doubt regarding the Output of the Program of Constructor Chaining I showed Below:

class Cube {

    int length;
    int breadth;
    int height;
    public int getVolume() {
        return (length * breadth * height);
    }
    Cube() {
        this(10, 10);
        System.out.println("Finished with Default Constructor of Cube");
    }
    Cube(int l, int b) {
        this(l, b, 10);
        System.out.println("Finished with Parameterized Constructor having
                                    2 params of Cube");
    }
    Cube(int l, int b, int h) {
        length = l;
        breadth = b;
        height = h;
        System.out.println("Finished with Parameterized Constructor having
                                    3 params of Cube");
    }
}

public class SpecialCube extends Cube {

    int weight;
    SpecialCube() {
        super();
        weight = 10;
    }
    SpecialCube(int l, int b) {
        this(l, b, 10);
        System.out.println("Finished with Parameterized Constructor having
                                    2 params of SpecialCube");
    }
    SpecialCube(int l, int b, int h) {
        super(l, b, h);
        weight = 20;
        System.out.println("Finished with Parameterized Constructor having
                                    3 params of SpecialCube");
    }
    public static void main(String[] args) {
        SpecialCube specialObj1 = new SpecialCube();
        SpecialCube specialObj2 = new SpecialCube(10, 20);
        System.out.println("Volume of SpecialCube1 is : "
                + specialObj1.getVolume());
        System.out.println("Weight of SpecialCube1 is : "
                + specialObj1.weight);
        System.out.println("Volume of SpecialCube2 is : "
                + specialObj2.getVolume());
        System.out.println("Weight of SpecialCube2 is : "
                + specialObj2.weight);
    }
}

OUTPUT:

Finished with Parameterized Constructor having 3 params of SpecialCube
Finished with Parameterized Constructor having 2 params of SpecialCube
Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20

The doubt is regarding the OutPut that how “1000”, “10”, “2000” & “20” are achiueved?

In the main Class We have created two Objects :

SpecialCube specialObj1 = new SpecialCube();
SpecialCube specialObj2 = new SpecialCube(10, 20);

First with “No Parameters” and Second with “Two Parameters”, The First Constructor Cube() with “No Parameter” has only two values this(10,10) and the one with “Two Parameters” has the values

Cube(int l, int b) 
    {this(l, b, 10);}

I don’t understand how the Below OutPuts are generated.

Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20

Please Can anybody help me!

Thanks,
david

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:42:27+00:00Added an answer on May 18, 2026 at 3:42 am

    I think it is obvious.
    specialObj1 is created during default constructor that invokes 1 argument constructor, that invokes 3 argument constructor. Each invocation sends 10 as value of cube dimension. So, volume is 10*10*10 = 1000;

    weight is not method. It is field that is initialized in default and 3-arg constructor. Due to first thing the constructor does is call of this(…) it does not matter what value is assigned to this variable in other constructors. The first constructor in chain actually overrides all previously set values. This is a reason why weight = 10 for the first object.

    Second object is created using 2-arg constructor that is called with parameters 10 and 20, so volume is 10*20*10 = 2000. (second 10 is set when 2args constructor calls 3args constructor.)
    In case of second object the weight is set by 3-args constructor, because 2-args constructor does not override this value.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava a class called DBhelper, and now I created a new class called
I hava the sql as below: select a.dept, a.name from students a group by
I hava a JFrame with multiple JPanels of similar width aligned one below other.
i hava a class that hold a list for each request but each request
I hava a generic class called ArrayBag. I want to override equals method. So
I hava a java program, a section of it is compute intensive, like this
I hava a supeclass called Car with 3 subclasses. class Ford extends Car{ }
I hava a Java program that needs to monitor a directory tree for changes.
I hava a fragment named DetailFragment which extends Fragment class with neccessary Override methods:
I hava a project which will look like similar to Paint program. Here, I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.