I’ve been learning C# over the past while, and as a result, I’ve been learning a lot about Object Oriented Programming concepts. I have a rather good understanding of classes and inheritance and am about to learn about polymorphism, but one concept I currently can’t wrap my head around is encapsulation. I just can’t understand what it does or why it’s even used in any sense. Any help would be appreciated, because I’d rather move forward having a good understanding of every c# concept I’ve touched on so far before moving on to new things.
In: Other
Encapsulation — literally “putting something in a capsule”. The concept of putting all sorts of complicated and messy innards into a pretty box with easy controls on the exterior.
Think of say, a car. Driving it may cause all sorts of complicated interactions, down to very subtle adjustments to engine timing or other engine management. But you don’t have to be concerned about that because you have a simple interface to interact with, such as a gas pedal. What the pedal does behind the scenes isn’t something you should worry about.
So encapsulation is simply the idea that `car.accelerate()` can internally be 30000 lines of code, and none of that concerns the user of the Car class. It’s all hidden from them.
Latest Answers