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
10
votes
10 answers

Java: how to handle a LOT of fields and their encapsulation cleanly?

Let's say I am tasked with coding some kind of an RPG. This means that, for example, I'll want to track a Character GameCharacter and stats thereof, like intelligence, damage bonuses or hitpoints. I'm positively scared that by the end of the project…
badp
  • 11,409
  • 3
  • 61
  • 89
10
votes
3 answers

Enforcing API boundaries at the Module (Distribution?) level

How do I structure Raku code so that certain symbols are public within the the library I am writing, but not public to users of the library? (I'm saying "library" to avoid the terms "distribution" and "module", which the docs sometimes use in…
codesections
  • 8,900
  • 16
  • 50
10
votes
6 answers

How can i restrict my clients with selected methods from the class?

Let's say I have 1 complete class with around 20 methods which provide different functionalities. Now we have multiple clients using this class, but we want them to have restricted access. For e.g. - Client 1 - Gets access to…
Thinker
  • 6,820
  • 9
  • 27
  • 54
10
votes
1 answer

Haskell Export Record for Read Access Only

I have a Haskell type that uses record syntax. data Foo a = Foo { getDims :: (Int, Int), getData :: [a] } I don't want to export the Foo value constructor, so that the user can't construct invalid objects. However, I would like to export getDims,…
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
10
votes
2 answers

Python descriptor protocol analog in other languages?

Is there something like the Python descriptor protocol implemented in other languages? It seems like a nice way to increase modularity/encapsulation without bloating your containing class' implementation, but I've never heard of a similar thing in…
cdleary
  • 69,512
  • 53
  • 163
  • 191
10
votes
1 answer

Netbeans warning: Exporting non-public type through public API

I'm creating a Slick2D game. Right now, I'm creating a Video class, which contains inner classes (FrameSize, FPS, FullScreen..). So I had an OOD idea to crate in way, like we call System.out.println(). That means that I will have public Video class…
nellykvist
  • 125
  • 1
  • 1
  • 8
10
votes
5 answers

Protect from adding object to NSMutableArray in public interface

I want to protect access to NSMutableArray in public interface I am trying to do this by defining property as NSArray in public interface and as NSMutableArray in private interface like this: @interface Order : NSObject @property (readonly, strong,…
Vladimir
  • 4,782
  • 7
  • 35
  • 56
10
votes
2 answers

Is there any tool or add-in for Delphi that I can use to help refactor code that is Not Object Oriented?

I have a large codebase which I am working with which has units like this: unit myformunit; interface type TMyForm = class(Form) end; procedure not_a_method1; procedure not_a_method2; var global1,global2,global3:Integer; ... In…
Warren P
  • 65,725
  • 40
  • 181
  • 316
9
votes
7 answers

Ruby : how to prevent modification of an array instance variable through an attribute reader

sorry for this noob question... let's say we have : class TestMe attr_reader :array def initialize @array = (1..10).to_a end end it is then possible to do : >> a = TestMe.new => #
m_x
  • 12,357
  • 7
  • 46
  • 60
9
votes
3 answers

Namespace or Class, which is better for encapsulation of only function members

So, let's say I have a few functions to deal with opening/closing of files. Is it better to make a class with all these function declared statically or simply put the "public" function in the header file of namespace "file", and put the rest…
Jimmy Lu
  • 4,810
  • 7
  • 25
  • 30
9
votes
3 answers

Java secondary not public Class usage produces error "Type is not Visible" even if accessed methods are public in Main class

I have a Main.java file: public class Main{ private EntityDrawer entityDrawer; public void setEntityDrawer(EntityDrawer entityDrawer) { this.entityDrawer = entityDrawer; } public EntityDrawer getEntityDrawer() { return…
will824
  • 2,203
  • 4
  • 27
  • 29
9
votes
5 answers

Data Encapsulation in Perl?

Hello Perl community on SO. I am using Perl since a few years, but since I am following SO, I recognized that I know Perl not enough. I wrote I quite big script over the past 4 years and tried to do this in OO style. I know that Perl<6 is not really…
stema
  • 90,351
  • 20
  • 107
  • 135
9
votes
12 answers

Is a function an example of encapsulation?

By putting functionality into a function, does that alone constitute an example of encapsulation or do you need to use objects to have encapsulation? I'm trying to understand the concept of encapsulation. What I thought was if I go from something…
Willem Obst
9
votes
2 answers

Does Kotlin break the rule of encapsulation?

Variables are used public in classes using the default visibility modifier. A setter and a getter is created for every member variable, but in Kotlin you do for example: class Person { var name: String = "unknown" } fun main(args:…
kevvex
  • 305
  • 3
  • 12
9
votes
6 answers

Do Ruby's "Open Classes" break encapsulation?

In Ruby, programmers are allowed to change predefined classes. So a really bad programmer could do something like: class String def ==(other) return true end end Obviously, almost no one would be quite this dumb, but the idea that more…
Daisy Sophia Hollman
  • 6,046
  • 6
  • 24
  • 35