Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Constructors not members of a class?

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?

like image 867
Pankaj Dwivedi Avatar asked Jun 14 '14 12:06

Pankaj Dwivedi


People also ask

Is a constructor a member of a class?

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.

Why constructor is not a member of class?

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();

Is constructor a member function?

A constructor is a special member function that is called whenever a new instance of a class is created.

Is constructor a part of class in Java?

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.


2 Answers

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?

like image 134
kevin Avatar answered Sep 18 '22 15:09

kevin


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.

like image 22
Jack Aidley Avatar answered Sep 19 '22 15:09

Jack Aidley