Questions tagged [encapsulation]

In OOP, mechanism for restricting access to some of the object's components or a design principle encouraging decoupling from implementation details.

In a programming language encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.

The second definition is motivated by the fact that in many OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined as a separate notion by those who prefer the second definition.

(quote from Wikipedia)

2032 questions
14
votes
4 answers

Is there any workaround for making a structure member somehow 'private' in C?

I am developing a simple library in C, for my own + some friends personal use. I am currently having a C structure with some members that should be somehow hidden from the rest of the application, as their use is only internal. Modifying by accident…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
14
votes
4 answers

Access of private field of another object in copy constructors - Really a problem?

In my Java application I have some copy-constructors like this public MyClass(MyClass src) { this.field1 = src.field1; this.field2 = src.field2; this.field3 = src.field3; ... } Now Netbeans 6.9 warns about this and I wonder what is…
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
14
votes
8 answers

What methods are there to modularize C code?

What methods, practices and conventions do you know of to modularize C code as a project grows in size?
Ari Ronen
  • 2,222
  • 2
  • 22
  • 24
14
votes
6 answers

A very common C# pattern that breaks a very fundamental OOP principle

Here is a very simple question, which I'm still very uneasy about: Why is it widely accepted now for a class to return a reference to its private member through an accessor method? Doesn't this totally break the encapsulation principle? If this is…
samus
  • 6,102
  • 6
  • 31
  • 69
13
votes
4 answers

Why does PHP allow protected and private methods to be made public via an override in subclasses?

From some brief fiddling about, I find I get an error when overriding superclass methods in a subclass when I do the following: Override a protected superclass method with a private subclass method Override a public superclass method with a…
Matt Gibson
  • 14,616
  • 7
  • 47
  • 79
13
votes
5 answers

Why do I need to use get and set?

I have a code segment: public class MyClass { private string _myProperty; public string MyProperty { get { return _myProperty; } set { _myProperty = value; } …
Simsons
  • 12,295
  • 42
  • 153
  • 269
13
votes
7 answers

When should a class use its own getters/setters vs accessing the members directly?

When generating setters and getters in Eclipse one of the options is to use the getters and setters within the class rather than accessing the class members directly. Is this level of class internal encapsulation useful or is it taking a good idea…
stimms
  • 42,945
  • 30
  • 96
  • 149
13
votes
9 answers

What is the antonym of encapsulation?

Using online dictionary tools doesn't really help. I think the way encapsulate is use in computer science doesn't exactly match its meaning in plain English. What is the antonym of computer science's version of encaspulate? More specifically, what…
deft_code
  • 57,255
  • 29
  • 141
  • 224
13
votes
2 answers

Testing properties with private setters

Currently in a part of my project a domain object like below exists: public class Address { public virtual string HouseName { get; set; } public virtual string HouseNumber { get; set; } public virtual string RoadName { get; set; } …
FLSH
  • 343
  • 1
  • 5
  • 15
13
votes
7 answers

Creating classes with a lot of imported functions here and there

Let's say i have a lot of functions in alotoffunc.py that is used by more than 1 type of object. Let's say ObjectI and ObjectII and ObjectXI all uses some functions in alotoffunc.py. And each of the Object were using different set of functions but…
alvas
  • 115,346
  • 109
  • 446
  • 738
13
votes
6 answers

Check if class is derived from a specific class (compile, runtime both answers available)

It is easier to explain on an example so, class base { //.... } class derived1 : public base { //... } In my library, there is a pointer of base class. The user of the library have to make classes derived from either base or derived1 and assign…
khajvah
  • 4,889
  • 9
  • 41
  • 63
13
votes
2 answers

Allow access to but prevent instantiation of a nested class by external classes

I'm looking to define a nested class that is accessible to the container class and external classes, but I want to control instantiation of the nested class, such that only instances of the container class can create new instances of the nested…
Dan Stevens
  • 6,392
  • 10
  • 49
  • 68
13
votes
7 answers

Will inheritance break encapsulation?

Let's say I have a CSV file and I create a class called CsvFile which extends from java.io.File. This class can parse a CSV file and return some data like how many columns are in the file. It can also be used for functions which takes java.io.File…
Frank
  • 7,235
  • 9
  • 46
  • 56
12
votes
3 answers

What is the difference between Public Property, Friend and Public Variable in VB6

OK so I understand that ion VB6, encapsulated properties in a class can belong to one of three categories: Public Property Friend Public Variable What is the difference between these and how do these compare to public and private properties in a…
JMK
  • 27,273
  • 52
  • 163
  • 280
12
votes
3 answers

C# Object reference not set to an instance of an object. Instantiating Class within a List?

public class OrderItem { public string ProductName { get; private set; } public decimal LatestPrice { get; private set; } public int Quantity { get; private set; } public decimal TotalOrder { get {return LatestPrice * Quantity;}} …
Taemint
  • 141
  • 1
  • 1
  • 6