This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many number of Objects created

 
Ankur Mahajan
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have small doubt regarding, number of objects created in case when a sub class is instantiated.

e.g.


Now the question is, when constructor of class B is called it will call the constructor of Class A and then from A contructor of Object class is created.

So a total of 3 objects should be created in heap.
Please some validate the same.

If this is true then how it is done in case of abstract class as its objects cannot be created.


TIA
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Ankur,

This is not the way you are percieving.Actually Only 1 object will be created in this scenario and the constructor calls will be same as you have stated.

constructor call is required to initialize those members of the super classes which are derived from the super class in derived class.
As the data members which are being derived into the derived class need to be initialized by their respective constructors(constructors are not inherited).

In case of Abstract class ,its true that they can't be instantiated but if some class has extended it in that case the members of that abstract class derived into the Sub class need to be initialized by the constructor of Abstract class only.

Hope you understand my point.


Regards...
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just Remember this, When an Object is Created, its constructor is called. But when a constructor is called, it does not mean that an Object is created.

Consider the following scenario

class A {
int x;

A()
{ this(10);
}
A(int z)

{
x=z;

}

public static void main(String args[])

{
A x = new A();

}

}

In the above case, the default constructor calls the overloaded constructor.
So in all 2 constructors are called, but Number of Object created is 1.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chander shivdasani:
In the above case, the default constructor calls the overloaded constructor.
So in all 2 constructors are called, but Number of Object created is 1.


LOOKS GOOD
 
Ankur Mahajan
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your replies.
I have understood the concept now.
 
Gaurav tyagigaurav
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!!!
I had exactly the same doubt that is stated in the original Q. Didnt know where to look for. Thanks to Java Ranch found the soln. here. Java ranch rocks [ ]. I however in the mean time was wondering how could i write/test the code to find out how many objects get created. Any idea how can we modify the above code to find out the number of objects created?

Cheers
Gaurav
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how could i write/test the code to find out how many objects get created.


The simple way i think is to use a initialization blocks,Which runs every time when a class object is created.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic