• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects will be created in Heap when we have multiple instances of a specific object?

 
Srinivasa Kumar Meda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that the Objects in Java are stored in Heap. If we have two instances of a specific class, how many objects will be available in heap?

For ex,

Sample s1 = new Sample();
Sample s2 = new Sample();

s1,s2 are two instances of Sample.

1). Can you please tell me how many Objects of Sample is created in Heap to store s1 and s2?


The second question is:

If class B extends class A and

A a = new B(); is declared.

2). What is the object stored for reference a in Heap??
3). and since B extends A, how the object of B will be in Heap??


Can someone please answer the 3 questions??

Thanks,
Srinivasa Kumar Meda
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivasa Kumar Meda wrote:We know that the Objects in Java are stored in Heap. If we have two instances of a specific class, how many objects will be available in heap?

For ex,

Sample s1 = new Sample();
Sample s2 = new Sample();

s1,s2 are two instances of Sample.

1). Can you please tell me how many Objects of Sample is created in Heap to store s1 and s2?


s1 and s2 are NOT the instances, but they are references which refer to those 2 instances created using new Sample(). By references i mean- You can use these- s1,s2- to access the fields and methods of the instances. And with Every new- we get one object on the heap.

Srinivasa Kumar Meda wrote:
The second question is:

If class B extends class A and

A a = new B(); is declared.

2). What is the object stored for reference a in Heap??
3). and since B extends A, how the object of B will be in Heap??

Here- Reference type for "a" is "A", but the Object created is of type- B. Inheritance has nothing to do with- which class's Instance is created. So if you do-


You can always assign the instances of the class and its subclass to a reference of the class type. (Like the way its done in the code provided by you).
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
every call of new Sample() create a new object....so every time when you write the new Sample(), object is different from previous one.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch, Srinivasa Kumar Meda
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

When you say new B(); one object is created with the word "new." The type of that object is B (the constructor following "new").

In the process of creating that object, constructors are called from the top down. So the resulting instance IS-AN Object, and IS-AN A, and IS-A B.

Therefore, a reference to that object can be assigned to a variable of type Object, or type A, or type B.

So when you say A a = new B(); you have a reference of type A which is pointing to an object of type B. And that's perfectly fine, because the object B is also an A.

So here's a quiz. If you do this...

...then how many objects are there? And what are the types of those objects?

How many references are there? And what are the types of those references?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are four types of reference variable.

three types of A and one types of B.

and objects are two....am i right marc???
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thakur Sachin Singh wrote:there are four types of reference variable.
three types of A and one types of B.

Correct. To be more clear, four reference variables, three of them are of type "A" and one is of type "B".
and objects are two....am i right marc???

Correct.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here we store one object in three reference variable a1,a3 and b1 and other object store only in a2.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thakur Sachin Singh wrote:here we store one object in three reference variable a1,a3 and b1 and other object store only in a2.


Just putting it in a right way- Three reference variables refer to the same one object and a2 refers to a different object. Its sometimes confusing if we say references store the object (Actually they store the objects address).
 
Srinivasa Kumar Meda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your responses. Its really informative and helpful in getting the answer for my question.

I have one associative question for this question.

The question is

Class B extends A{
}

here B is a sub class of A. i.e., B IS-a A.

How the object B is stored in Heap?? How B IS-a A can be represented in Heap? Is the Object B having A inside it while creating object B in Heap??
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivasa Kumar Meda wrote:...How the object B is stored in Heap?? How B IS-a A can be represented in Heap? Is the Object B having A inside it while creating object B in Heap??

Only one object created but everything inherited from A also contained there. That's why there should be a call to super (directly/indirectly) within the constructor.
 
Srinivasa Kumar Meda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijitha,

Thanks for the reply.

You mean all the properties and methods of object A are part of the object B when the Object B is created in Heap?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivasa Kumar Meda wrote:...You mean all the properties and methods of object A are part of the object B when the Object B is created in Heap?

All the super class(es') properties will be initialized when the constructor gets completed. You can read more here about the initialization.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apurva Gaonkar,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic