aspose file tools*
The moose likes Beginning Java and the fly likes how to check instances/object created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check instances/object created" Watch "how to check instances/object created" New topic
Author

how to check instances/object created

Soni Mitesh
Ranch Hand

Joined: Sep 02, 2007
Posts: 33
hello ranchers,

class A{
}

class B extends A{
}

class C extends B{
C c=new C();
}

Scenarios.

1) my confusion is how many instances will be created?

2) if any of the super class will be having CONSTRUCTOR and if it is being called in sub class (here 'C') using super(); method in C class's constructor, now how many instances will be created?

instances means object.
Thumb rule, constructors are not inheritable in java.

is there any way to write a code to check that constructor or object is created?

class A{
A(){
System.out.println("hello from A");
}
}

class B extends A{
B(){
System.out.println("hello from B");
}
}

class C extends B{
C c=new C();
}
here in this case both the constructor printed the messages perfectly!!

Thanks.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

please UseCodeTags to post the code

Update: One new- will create one instance( no matter if its extending some class or implementing some interface).


Mohamed Sanaulla | My Blog
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Although that code looks dangerously like every C object contains a new C object. I'd expect you to get a stack overflow as soon as you call new C().
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Matthew Brown wrote:Although that code looks dangerously like every C object contains a new C object. I'd expect you to get a stack overflow as soon as you call new C().


Oh yeah, I didnt see that- dangerous indeed. probably the Code tags would have helped me spot that ;)
Soni Mitesh
Ranch Hand

Joined: Sep 02, 2007
Posts: 33
after doing C c=new C();
why the constructor of extended class B(){
B(){
System.out.println("hello from B");
}
}

gets printed on the console?
could you please help me with constructor (can't-inherit) funda?

Thanks
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

There's an implicit call to super() in your default constructor. And that is the reason why it prints the constructor from the B as well.
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi ,
you need to exercise the constructor chaining mechamism in java. This link will help you.LINK
After that, you will have the answer. you will be more happy since you find the answer by youself.

Thinker
Nuwan Arambage
Soni Mitesh
Ranch Hand

Joined: Sep 02, 2007
Posts: 33
I really appreciate for the replies,
and particularly the last one,
i like to search the ans by own, but i guess was missing guidelines.
thanks all Floats...
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Soni Mitesh wrote:I really appreciate for the replies,
and particularly the last one,
i like to search the ans by own, but i guess was missing guidelines.
thanks all Floats...


if you are happy, I'm happy too... you realize the way of how to learn......... There are so many friendly faces out there to help you when you got stuck..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check instances/object created
 
Similar Threads
order?
Do constructors create objects? (was: is it true?)
static block and empty block inside any class.
Constructors are not inhertied - True
hide vs override