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 ?
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/whatever..it 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