Neeraja
Neeraja

Reputation: 98

What part of OOP are constructors?

Do parameterised constructors fall under encapsulation or abstraction, or something else entirely as a part of object-oriented programming?

Upvotes: 2

Views: 315

Answers (2)

jaco0646
jaco0646

Reputation: 17066

While StephenC is technically correct that constructors are an abstraction of object initialization, I don't think that answer is particularly helpful in the context of OOP. After all, we could state that all software beyond binary ones and zeroes is an abstraction of other software.

In the context of OOP, we typically consider abstraction as the opposite of concretion: interface versus implementation. From that perspective, constructors are most certainly a concretion. You cannot construct an abstraction.

Constructors and encapsulation are orthogonal. A constructor can be used to create a pure data structure whose internal implementation is public. There is no encapsulation in this case.

In class-based object-oriented programming, a constructor simply confirms that nothing comes from nothing.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718778

First of all, Java constructors are what they are and do what they do, irrespective of how you categorize them.

My take is that constructors are clearly an example of abstraction in that they hide the algorithmic details of object initialization.

Since the constructor isn't actually the mechanism that hides the state of an object, they aren't an encapsulation mechanism. But obviously they work with the encapsulation mechanism; i.e. field access modifiers.
The above applies to both parameterized and non-parameterized constructors.

For more resources explaining the difference between encapsulation and abstraction, see:

Upvotes: 3

Related Questions