Are Constructors not members of a class? If they are, why cannot they be inherited?
JLS 7.0 says that constructors are not members and hence cannot be inherited. Is that true alone for Java or is it a general OOP paradigm?
Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.
Because by using a super class's constructor we can access/initialize private members of a class. A constructor cannot be called as a method. It is called when object of the class is created so it does not make sense of creating child class object using parent class constructor notation. i.e. Child c = new Parent();
A constructor is a special member function that is called whenever a new instance of a class is created.
All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.
Constructors are not inherited for a good design reason. If they are inherited, then you'd have problems trying to hide them in the inherited class.
Consider this example:
class Plane{
public Plane(int passengerCapacity, int cargoCapacity){...}
}
class F18 extends Plane{
public F18(){
super(0,0);
....
}
}
Now, does the following constructor make sense?
Plane p = new F18(0,0);
Not really, because the child class is a special case of the parent class. In this particular example, it does not make sense to provide the Plane constructors in the F18 class; the child class wishes to control how the parent class is constructed.
If constructors are inherited and someone wish to hide the parent constructors, they may attempt to do something like this:
class F18{
Plane innerPlane;
public F18(){
innerPlane = new Plane(0,0);
}
}
Not really a solution because in every method and property, one would have to write return innerPlane.SomeMethod();
, which pretty much defeats the purpose of inheritance.
This principals apply to other OOP languages (e.g. C#) as well.
But what if I really want to inherit constructors in many child class...
Then your architecture is wrong. You don't actually need inheritance to solve your problem. You can abstract the parts that are different in another base class / interface. e.g.
class Plane{
IPilot Pilot;
public Plane(int passengerCapacity, int cargoCapacity, IPilot pilot){...}
}
Constructors are ways to construct an object. Do you really need many child class which can be constructed in the same way (and using the same logic, since they're inherited), or a class which has the flexibility to swap out some of its components?
The concept of inheriting a constructor does not make sense in most paradigms; instead the constructors are called in reverse order of inheritance, so for example if you have:
Pidgeon -> Bird -> Animal
Where ->
indicates 'inherits from', then to construct a Pidgeon
it first calls the Animal
constructor, then the Bird
constructor and, finally, the Pidgeon
constructor. If it didn't do it this way then you would have to either initialise all members of the class you're inheriting from or manually call the constructor of the base class (as you may have to do anyway if you have specific parameters to pass).
Or, to put in another, whereas normally a function in the derived class replaces the function in the base class, constructors extend the function in the base class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With