File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes superclass constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "superclass constructors" Watch "superclass constructors" New topic

superclass constructors

Nancy Bradley
Ranch Hand

Joined: Feb 25, 2004
Posts: 32
Can some one tell me where can I find a clear explanation on how to call superclass constructors? implicit and explicit calls?
Nancy B
C. Nimo
Ranch Hand

Joined: Mar 23, 2004
Posts: 82
implicit calls are made for you. that's why they're called implicit.
this means that, suppose your superclass has a default constructor - it will
be called by the extending class's constructor (unless an explicit call is made).
explicit calls are made by you, as the programmer. when you write your constructor, and want to call a specific constructor of the super class. then you use the call super(arg0, arg1...) in your constructor. e.g.

in this code, the constructor for B calls the default no-arg constructor of A implicitly.

in here, if you call the constructor of B with an int argument, the constructor of A will be calles (explicitly).
however, if you try to create a B object with the no-arg constructor,
you'll be facing a problem.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
An important thing to recognize in all of this is that the first thing any constructor does is make a call to another constructor (ignoring what any constructor defined in the Object class does) - either another constructor in the same class, or a constructor in the superclass. This call to another constructor is required, and it must be the first thing that the constructor does.
If you don't specify that your constructor invokes another constructor with an explicit call to this(...) or super(...), then the compiler sticks in a call to super() as the first thing that your constructor does (implicitly).

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Nancy Bradley
Ranch Hand

Joined: Feb 25, 2004
Posts: 32
Thank you very much, your explanations made the concept clear.
Nancy B
I agree. Here's the link:
subject: superclass constructors
It's not a secret anymore!