Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check instances/object created

 
Soni Mitesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please UseCodeTags to post the code

Update: One new- will create one instance( no matter if its extending some class or implementing some interface).
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Soni Mitesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic