60
            
            votes
        
        
            
            
        Do you need to think about encapsulation if you can ensure immutability?
                    I hate how encapsulation is always framed as preventing unauthorized access. If this were the best way to think of it, immutability would indeed eliminate most of the need for encapsulation. In fact, ...
                
            
       
        
            
                54
            
            votes
        
            
                
                Accepted
            
        
            
            
        Do you need to think about encapsulation if you can ensure immutability?
                    The question
Casting your question to real life:
Is it okay for your doctor to post your private medical records publicly to Facebook, provided no one (other than you) is able to change it?
Is it ...
                
            
       
        
            
                47
            
            votes
        
        
            
            
        Is it a bad idea have make a class method that is passed class variables?
                    Calling a class method with some class variables is not necessarily bad.  But doing so from outside the class is a very bad idea and suggests a fundamental flaw in your OO design, namely the absence ...
                
            
       
        
            
                33
            
            votes
        
            
                
                Accepted
            
        
            
        Is it a bad idea have make a class method that is passed class variables?
                    There are may things with the class that I would do differently, but to answer the direct question, my answer would be 
yes, it is a bad idea
My main reason for this is that you have no control over ...
                
            
       
        
            
                33
            
            votes
        
        
            
            
        Do you need to think about encapsulation if you can ensure immutability?
                    Encapsulation could mean that you hide the actual storage of immutable data.
E.g.:
class Color
{
  private readonly uint argb;
  public byte Blue => (byte)(argb & 0xFF);
  public Color(byte ...
                
            
       
        
            
                24
            
            votes
        
            
                
                Accepted
            
        
            
            
        How does encapsulation actually work?
                    Coupling
  ClassA relies upon the interface only, delegating this responsibility of passing the classB object elsewhere                                                            
This is the idea. If ...
                
            
       
        
            
                22
            
            votes
        
        
            
            
        How does encapsulation actually work?
                    Your confusion is probably caused by focusing on meaningless class and interface names, with no real usage scenario behind it. So better let us make a concrete example (I prefer C#, but it is not ...
                
            
       
        
            
                18
            
            votes
        
            
                
                Accepted
            
        
            
            
        C# encapsulate field is a violation of YAGNI
                    There are two completely different kinds of software: libraries that need a stable binary interface across multiple versions, and applications or internal software where you can just refactor.
For ...
                
            
       
        
            
                17
            
            votes
        
            
                
                Accepted
            
        
            
            
        Can method names give any implementation details and break encapsulation?
                    The actual inventory is still hidden from outside classes (i.e., the List<GameItem> inventory is private), so encapsulation is not broken. Whether it makes more sense to name the method has(...
                
            
       
        
            
                17
            
            votes
        
            
                
                Accepted
            
        
            
        Can renaming a method preserve encapsulation?
                    I think you are missing the point. Its not saying you should rename the setter and getter, but to have methods which add and remove items from the fridge. ie
public class Fridge
{
    private int ...
                
            
       
        
            
                14
            
            votes
        
            
                
                Accepted
            
        
            
        Is it okay to have misleading struct and function names for the sake of encapsulation?
                    This is why we invented the word meta. You're packing the result structure with meta info that isn't strictly part of what you're returning. But the info is about what you're returning.
Just carve out ...
                
            
       
        
            
                13
            
            votes
        
            
                
                Accepted
            
        
            
            
        Are there any legitimate use cases for protected visibility?
                    In short
The protected visibility is a legacy that was designed long before Barbara Liskov's LSP (more precisely its history constraint) demonstrated its many drawbacks.
Its only meaningful use is for ...
                
            
       
        
            
                12
            
            votes
        
            
                
                Accepted
            
        
            
        Separation of concerns: When is it "too much" separation?
                    Your various examples of splitting out concerns into separate functions all suffer from the same issue: you are still hard-coding the file dependency into get_last_appearance_of_keyword. This makes ...
                
            
       
        
            
                12
            
            votes
        
            
                
                Accepted
            
        
            
        Encapsulation violation
                    Returning a pointer to an internal variable of a class is an encapsulation violation, once you have that pointer, you can modify the speed variable inside of car.
To avoid that, return the value of ...
                
            
       
        
            
                11
            
            votes
        
        
            
            
        Should a class provide public mutators for all its private fields?
                    So I did some googling.  Here is the rule you quote:
  To respect OO encapsulation concepts, private fields should always be
  accessed through accessors
https://www.appmarq.com/public/changeability,...
                
            
       
        
            
                10
            
            votes
        
        
            
        Is the Liskov Substitution Principle concerned with protecting state invariants?
                    No, I don't think so.
LSP says that S can substitute for T.  What that means in practice is that S fulfills the same contractual obligations that T does.  The writer of S can expose internal state (...
                
            
       
        
            
                10
            
            votes
        
        
            
            
        C# encapsulate field is a violation of YAGNI
                    YAGNI isnt a principle, its an excuse for not doing stuff.
Here we can use the syntactic sugar
public DataAccess Service { get; set; }
and its cost us 10 characters worth of typing to get the ...
                
            
       
        
            
                9
            
            votes
        
        
            
        What is the best practice for a regression test that wants to know about encapsulated information?
                    You wrote 
  Alpha constructs a new Bravo class during its own constructor and exposes no public visibility to it
So whatever Alpha does, using Bravo for it is an internal implementation detail, right?...
                
            
       
        
            
                8
            
            votes
        
        
            
        Is it appropriate to process a property in the setter?
                    This breaks the principle of least surprise. For a property with a getter and setter, you would assume them to be symmetric, i.e. you get the same thing as you set, at least semantically.
Here you set ...
                
            
       
        
            
                8
            
            votes
        
        
        The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?
                    Identify your single source of truth. That's what has the authority.
If your program is the authority on what can and can't happen with an employee then the DB is nothing but a way to persist ...
                
            
       
        
            
                8
            
            votes
        
        
            
            
        Do you need to think about encapsulation if you can ensure immutability?
                    If you can guarantee Immutability, do you need to think about Encapsulation?
Perhaps, but probably not in a way most people think.
First, realize that encapsulation (the practice of hiding the ...
                
            
       
        
            
                7
            
            votes
        
        
            
        Is it a bad idea have make a class method that is passed class variables?
                    When the intention of this design is that you want to be able to reuse this method for data which does not come from this class instance, then you might want to declare it as a static method.
A ...
                
            
       
        
            
                7
            
            votes
        
        
            
        Should a class provide public mutators for all its private fields?
                    No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".
                
            
       
        
            
                7
            
            votes
        
        
            
            
        Is there anything wrong with writing getter/setter methods in C#?
                    C# properties are made of methods. Thus, by using methods instead of properties, you do not get any runtime advantages.
Instead it will be all about developer productivity and readability of your ...
                
            
       
        
            
                7
            
            votes
        
        
            
        Is it okay to have misleading struct and function names for the sake of encapsulation?
                    I don't see why you consider it "dirty" to keep the original name, as that's exactly what OOP does - there's a public interface to an object, and there are some internal implementation ...
                
            
       
        
            
                7
            
            votes
        
            
                
                Accepted
            
        
            
            
        Confused on how abstraction and encapsulation is helpful
                    the function doesn't rely on assimp as much meaning it would be easier to change the assimp code or replace it with another library, but I still find it confusing how it's useful or helpful it just ...
                
            
       
        
            
                6
            
            votes
        
        
            
        Does encapsulation in OOP happen at run time or compile time?
                    Encapsulation is a design principle. It's about writing software, has nothing to do with running or compiling software.
To address the two concrete questions:
For the main implementation of Python (...
                
            
       
        
            
                5
            
            votes
        
        
            
        A problem of decoupling
                    If it is acceptable that the Shape classes know how to draw themselves onto something (without knowing which output device or graphics library is used underneath), then you could introduce a ...
                
            
       
        
            
                5
            
            votes
        
        
            
            
        Is it appropriate to process a property in the setter?
                    No, this is not fine; however, it is not the worst thing in the world either. I would reject a PR with this in it, but I would not get mad at you ;)
Let's say you deploy this to production... a few ...
                
            
       
        Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
encapsulation × 211object-oriented × 78
java × 30
c# × 29
object-oriented-design × 29
design-patterns × 28
design × 24
c++ × 21
abstraction × 14
inheritance × 10
c × 9
class-design × 9
architecture × 8
javascript × 8
single-responsibility × 8
access-modifiers × 8
programming-practices × 7
separation-of-concerns × 7
refactoring × 6
getters × 6
php × 5
python × 5
interfaces × 5
clean-code × 5
class × 5
 
         
         
         
         
         
         
         
         
         
         
         
        