• 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

Really confused now about constructors and subclasses

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just came across a question that has totally thrown my understanding for a loop. Here is the code.

My understanding is this, Base class does not have a constructor (that is a trick method in there). In extends Base. In doesn't have an explicit constructor too, so it creates a default constructor, or rather the compiler does. This default constructor, presumably will call the super() on it's superClass. However my understanding was that if you have a subClass that calls super(), the superClass has to have a () constructor, that is, it can not rely on the default constructor to handle that. I could swear I've run code to test that theory out. But I just ran the code above, and sure enough it doesn't through a compile-time error. I am really confused now. Am I somehow confusing this situation with another situation? Please help somebody.
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I think I see why now...boy I almost freaked out. I think in all the previous cases that I created, the superClass had other constructors but no () constructors. Subsequently any creation of a constructor (with parameters), will therefore make the compiler not create a default constructor.
Now my subClasses had a multitude of constructors with different number of parameters. And those constructors, by default called the () constructor. And the superClass didn't have a () constructor because other constructors with parameters were already specified.
But in the above case, since neither the superClass nor the subClass have explicit constructors, the default constructor WILL be created for both classes, and therefore everything will be fine.
[ May 10, 2003: Message edited by: Wilson Mui ]
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Wilson
And yes,in this case,if the subclass call the superclass default constructor if the superclass without the empty parameter constructor superclass(),you will get the Compiler error.
reply
    Bookmark Topic Watch Topic
  • New Topic