This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Creating an instance of a sub-class runs parent and child constructors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an instance of a sub-class runs parent and child constructors?" Watch "Creating an instance of a sub-class runs parent and child constructors?" New topic
Author

Creating an instance of a sub-class runs parent and child constructors?

Andrew Mast
Greenhorn

Joined: Dec 22, 2007
Posts: 4
Hi,

I'm learning Java basics and while playing around with inheritance, I found something that I cannot understand.

(Below) I created two classes (class1 and class2) and class2 extends class1. Now, when I instantiate an object of sub class2 in my main method and run the code both constructors are run, because I'm seeing the println statements from both class' constructors in my output.

Is it just a simple fact that when you instantiate an object that extends another object, it runs both child and parent constructors? Maybe I'm missing something.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Welcome to the Ranch.

Yes, you do have to call both constructors.

Go and look at your subclass. You can have methods in it which aren't in the superclass, and fields which aren't in the superclass. But you also have fields from the superclass and methods from the superclass. So whenever you instantiate the subclass, you have to set up the superclass fields too. To do this, you call the superclass constructor from the subclass constructor. That is what super() means.
Andrew Mast
Greenhorn

Joined: Dec 22, 2007
Posts: 4
Thanks! Ok, that makes sense... so because I'm instantiating the sub-class which is part of the super, the super needs to be 'set up' too.

Ok, the only question remaining is because you said that, "To do this (set up superclass), you call the superclass constructor from the subclass constructor.(i.e. super())"

But, I actually never explicitly called super(), so am I right to assume that super() is run regardless, even if I don't explicitly call the superclass constructor from the subclass?

Thanks again!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Only too pleased to be of help . . .

It is probably an inaccurate thing to say, but there is a part of the subclass which comes from the superclass, not a part of the superclass which comes from the subclass.

You presumably know that you have to write super(something); as the first line of the constructor in the subclass. There are exceptions:
  • if you have an overloaded constructor, when you can start with this(something); in some of the constructors.
  • If you have a no-arguments constructor in the superclass, then the compiler or JVM will "assume" you have an implicit "super();" in your constructor, even if you haven't written it.
  • Yes, there is an implicit super(); call if you don't write it yourself. If there is a super(); call and the superclass constructor requires any parameters, you have to supply arguments to match those parameters, otherwise you get a compiler error saying it can't find the constructor.
    [ December 22, 2007: Message edited by: Campbell Ritchie ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Creating an instance of a sub-class runs parent and child constructors?
     
    Similar Threads
    Query on Q49, RHE Bonus 1 - final 'default' method overriding
    Accessing component from main class.... Help!
    Package & Compilation Problem
    query on method over riding
    inner anonymous class fields initialization