Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
61 views
in Technique[技术] by (71.8m points)

java - 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?

question from:https://stackoverflow.com/questions/65932516/what-part-of-oop-are-constructors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...