Questions tagged [encapsulation]

Encapsulation is the principle of bundling software elements in a way to hide implementation details and to expose only a known interface.

Encapsulation is the principle of bundling software elements in a way to hide implementation details and to expose only a known interface.

The concept was first exposed by D. Parnas in the context of modular software development. It was at the origin of the module concept (e.g. adopted in modula-2, or as packages in ADA83), and became the corner stone of OOP classes.

See also:

208 questions
7
votes
10 answers

Why combining getters, setters and private access modifier is not enough to hide implementation?

In Clean Code: public class Point { public double x; public double y; } The author wrote about the above Point() class: This exposes implementation. Indeed, it would expose implementation even if the variables were private and we were…
6
votes
6 answers

What is the usefulness of private variables?

While I've never used a language that had built-in variable privacy, a book I'm reading by Douglas Crockford explains a way to create privacy in JavaScript, however it doesn't make sense to me so. The term "private" isn't something I think of as…
J.Todd
  • 3,823
4
votes
1 answer

Encapsulation: Should all properties be private and protected?

After learning about encapsulation in OOP, I started to use only protected and private properties, share data via accessors and change properties via mutators. Right now, I'm afraid of declaring public properties. Is it good to keep everything…
volter9
  • 156
  • 5
1
vote
1 answer

Encapsulate downcasting when returning from a method

In chapter 24 of Code Complete the author says, in reference to encapsulate downcasting when returning from a method, "If a routine returns an object, it normally should return the most specific type of object it knows about. This is particurlarly…
user1323245
  • 461
  • 2
  • 8
0
votes
0 answers

Return compound structure from getter method in DTO

Suppose the following DTO class. Which of the two getters breaks encapsulation least? class Foo { public: /*Return the most primitive type. Caller do not need to worry about what Array is*/ const RecType*…