This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

superclass constructors

 
Nancy Bradley
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one tell me where can I find a clear explanation on how to call superclass constructors? implicit and explicit calls?
Thanks
Nancy B
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
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.
Nimo.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Nancy Bradley
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, your explanations made the concept clear.
Nancy B
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic