Encapsulation

Encapsulation

As a doctor, I want to have something simple that I can give to my patient which will
Combine the all of the patient’s vitamin needs.
I would also like some of the vitamins to have an extended release,
so that they can take it just once per day.             

We want to bundle the data (vitamins), along with the methods which operate on that data [time-release, the patient running takeVitamin() function, etc].

The benefit of this action simplifies the medication to the patient by making it easier to work with.
Unless they want to, they do not need to know all the vitamin chemical names, or how gradually certain ones release. They do not need to know that ‘private’ stuff in order to interact with it.
In programming you can think of the ‘class’ as a capsule. The patient does not need to know the inner details about the capsule in order to use it. Simply take medication, and this result is expected.

This also describes encapsulation in programming.
~~
First let’s mention what you find inside a ‘.cs’ (C Sharp) file.

In C# you will notice that as you open the file you see a couple common things.
The stuff at the top is called ‘using’ statements. These statements let you know what other pieces of code have been made part of this program.

Afterwards, you will find something called the ‘namespace’.
Namespace, is just a space where you can publicly name functions and objects within that space names must be unique, however, between namespaces the same name could exist.
For instance, you can have two controls named TabControl if one belongs to the DevExpress namespace where the other belongs to the Windows namespace.

The next thing you will notice in the file is where we begin our connection to encapsulation.
Class; a class is a recipe or template. Once you write the recipe the system can make those ‘objects’. An object will have an internal state (variables and data), as well as behaviors (functions).

The code description in the class encapsulates the idea of the object, and how the object works.

A class is just one example of encapsulation;
this term in computer science has a wide array of applications.

Data Hiding

As I hinted towards this with the time-release vitamin analogy, sometimes encapsulation is about hiding data. The functions which helped make the timerelease() routine work can be hidden. Before a class, function or variable is named and given a type which represents it, you will see something called an Access Modifier. This will let us know if the information is private, or publicly accessible. Please note that there are more access modifiers than these two; however, these two are the most common for which we will start with.

Also, let me know if there are any topics you would like to see covered outside of the fundamentals, such as going more in-depth into access modifier.

~~~~

Public – Means that the class, function or variable can be seen by something outside the class. So, like, I mentioned that class is a recipe. If the recipe created that object, which data or functions about that object should be available.


Private – Means that the class, function or variable cannot be seen or used outside the class.
So, code within the capsule can see this sort of data or functions, however, code outside of the capsule would not be aware of it.
~~~~~

o\_/o   Circular Reference Detected in regards to the term class and public.
So, in the case of a class being public or private… generally, no, you will not have a private class… unless you had nested a second class inside or some other scenarios. There are other access modifiers for a class, for instance, if you only want a class to be shown to the DLL (or assembly) in which it belongs then you might use the word ‘internal’ instead of ‘public’.


Assembly – what is a DLL or Assembly from a code perspective?
After writing your code you compile it into either an executable or a library. The executable you would run, but the library or assembly is something for other code to use. For instance, if you wrote some calculation code, you might produce an assembly for reuse in other projects. You might want the universe of your assembly to publicly see the data, but perhaps not a third-party company using the assembly, in this case you would use the access modifier of internal.

Sometimes in development it is important to hide some inner details of your object. By doing so, the capsules act like Lego blocks. Build an object, and hook it together with another. By doing this a developer can easily look at the object and know what data it shares, and what functions it exposes.

* NEW Computer Science Terms: Data Structure *

These capsules, and classes are an example of something called Data Structures, to which a whole topic should be dedicated. To simplify, a data structure is just a collection of data that has some rules. How is the data stored, indexed/organized, and how the data can be accessed. Data Structures also define relationships as to how the data interacts.


As we write code to organize data these objects which we produce are data structures. Encapsulation in object-oriented programming allows the things we produce to operate both onto the outside world, but also onto the world within their capsule.
The world of their inner state, and how it responds.

Leave a comment