Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

constructor chaning

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,20);
System.out.println("Finished with Parameterized Constructor having2 params of Cube");
}
Cube(int l, int b, int h) {
length = l;
breadth = b;
height = h;
System.out.println("Finished with Parameterized Constructor having 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 having2 params of SpecialCube");
}
SpecialCube(int l, int b, int h) {
super(l, b, h);
weight =20;
System.out.println("Finished with Parameterized Constructor having3 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 is

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

could someone please explain me how :"Weight of SpecialCube2 is : 20" is coming...

will appreciate any help from the core....
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pl quote you source code !!

This is what makes your out put 20..




 
Madan Mohan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys....even i could make out that...but what i am not able to Understand here is why everyconstructor in the class and super class is being called .... i think constructor should be overloaded based on the call...but here i see that all the constructor with diffirent arguments are being invoked...why so... please explain...
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the output I get is:

Finished with Parameterized Constructor having params of Cube
Finished with Parameterized Constructor having2 params of Cube
Finished with Default Constructor of Cube
Finished with Parameterized Constructor having params of Cube
Finished with Parameterized Constructor having3 params of SpecialCube
Finished with Parameterized Constructor having2 params of SpecialCube
Volume of SpecialCube1 is : 2000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20


Let's analyze creation of the first object:
1) first SpecialCube() is called
2) then super() calls no-arg constructor of superclass (which is Cube())
3) then this(10, 10) calls Cube(int l, int b)
4) then this(l,b,20); calls Cube(int l, int b, int h)
5) now "Finished with Parameterized Constructor having params of Cube" is outputted
6) comes back to Cube(int l, int b) and outputs: "Finished with Parameterized Constructor having2 params of Cube"
7) comes back to Cube() and outputs: Finished with Default Constructor of Cube
8) comes back to SpecialCube(), but there is no output

This is how the specialObj1 is created, you can anlyze the 2nd object similarly.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Madan Mohan wrote:thanks guys....even i could make out that...but what i am not able to Understand here is why everyconstructor in the class and super class is being called .... i think constructor should be overloaded based on the call...but here i see that all the constructor with diffirent arguments are being invoked...why so... please explain...



Here one constructor is calling another constructor using this(...) syntax.
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madan,

Please use code tags.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic