• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object creation with super() and constructor

 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between calling a superclass constructor using super() and new classname() or to put it this way, what really happens when the two statements are executed?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A call to super() which has to be the first statement in the constructor, creates [gross oversimplification] that part of the object which is inherited. In most subclasses a super() call is essential.

A new Superclass() call creates a new object of the superclass completely separate from where one is working at present.
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so a call to super() doesn't necessarily create a superclass object. Right?
does the superclass constructor run? absolutely, it will.

tell me whether i'm right.
a call to constructor doesn't create an object. it's only a syntactic way of telling the jvm that we need an object and also gives us a way to initialize instance variable. we can perform both operations simultaneously. that's the use of constructors. that is we are initializing the instance variables immediately after object creation.

am i right?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I agree with what you say. I find it a bit difficult to understand, however.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic