Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Superclass non parameterized constructor

Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have extended a BaseClass to SubClass. In BaseClass, I have defined parameterized consstructor, but there is no non parameterized constructor defined. Since I have defined parameterized constructor, non parameterized constructor will not be created automatically and I am required to define it.
If I dont create a non parameterized constructor in BaseClass and extend this BaseClass to SubClass. If I dont include this parameterized constructor in the first line of SUbClass constructor, will the compilation give an error ?
DOes it mean that, either I have to create non parameterized constructor in super class or include parameterized constructor in the constructor of the SubClass ?

Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get to understand things far very clearly, when you try them out yourself.

May be I can help you with the concept.
The first line of every constructor must be a call to super() - parametrized or non-parametrized, or this() - parametrized or non-parametrized. If you dont do either of this explicitly, the compiler inserts a simple 'super()' to the first line. It doesn't care whether your super class has a non- parametrized constructor or whether it is public/protected/ just inserts one. So, in that case you're going to get a compile error, when you don't have a non-parametrized constructor in your base class. If you're going to overload you're base class constructor, then take care you have a non-parametrized constructor or make sure your sub class calls explicitly the parametrized constructor in its constructor's first line. If you're not going to touch(overload) the base class's constructor at all then thats very fine, as the compiler shall provide you with a default constructor.
This is the only thing you'll have to watch out for. In your subclass, you might/might not have parametrized/non-parametrized constructors, and that really don't matter much - its ultimately the super class's constructor that is going to be invoked.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic