File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes object creation with super() and constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "object creation with super() and constructor" Watch "object creation with super() and constructor" New topic
Author

object creation with super() and constructor

krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
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

Joined: Oct 13, 2005
Posts: 38474
    
  23
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

Joined: Jul 25, 2006
Posts: 158
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

Joined: Oct 13, 2005
Posts: 38474
    
  23
I think I agree with what you say. I find it a bit difficult to understand, however.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object creation with super() and constructor