• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

superclass constructors

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, your explanations made the concept clear.
Nancy B
 
reply
    Bookmark Topic Watch Topic
  • New Topic