Possible Duplicate:
Java Constructor Inheritance
When creating subclasses,
- Why do I have to create a constructor and write super in every ctor?
- If my behavior is similiar to the superclass shouldnt I expect to inherit them as well?
Possible Duplicate:
Java Constructor Inheritance
When creating subclasses,
Constructors aren't inherited because you're not creating an instance of the superclass, you're creating an instance of a new class. There's no way to know which superclass constructor you want to call.
To be fair, the default (no arg constructor) always exists. It's the specific arg ctors that you're referring to, I'm assuming.
In reality, java always creates new constructors for your subclasses, but only for the default, no parameters constructor. If your class has constructors other than the default (no parameters) constructor, you have to define them again... Constructors are not meant to be behaviour methods but rather object initialization (which may change for subclasses with new attributes)
Default no argument Constructor is inherited and called by default. In case you like to call another one, you can use super() . In case default constructor can not be used, you have to use one4 of accessible super constructors
Constructors have the same name as the class name and if you're able to inherit them into the subclasses, they can no longer be constructors of subclasses. The default parameterless constructor is always inherited though.
Constructors are invoked to create objects from the class blueprint, ie, to initialise the data members of a class. If a subclass were to inherit a constructor,then while calling a subclass you need to have the knowledge about the parent class' data members too. That is not what one would see in real life scenario. For example, if you create an object of type Ferrari, you would definitely be interested about initialising parameters like speed,acceleration and you would not bother about initialising the parameters of a Car object, even though Ferrari inherits from Car. Therefore, while calling the constructor of a Ferrari, you would only and only be bothered about the members of Ferrari and not the members of its parent class. I hope to have made my point clear.