Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How many number of Objects created

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your replies.
I have understood the concept now.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic