I\'ve been attempting to learn C#.NET for the past month or so, and the array of
ID: 642715 • Letter: I
Question
I've been attempting to learn C#.NET for the past month or so, and the array of ideas that seems to always trip me up is encapsulation. As this is one of the three pillars of OOP, I feel that I am operating at a loss for not understanding their use and implementations more clearly. When learning, though, it is often useful to develop or assimilate mnemonics to assist in maintaining all this knowledge. Having a good reference manual on hand is one thing, but keeping a functioning base of understanding is another entirely.
When keeping track of whether a type/method is public, private, protected, static, or sealed, I find myself wondering what and why all at the same time.
My question, then is how do you go about remembering encapsulation keywords and when to use them? Trial and error is what is working for me now as a student, but I would hope to move beyond that before making professional use of this skill.
Explanation / Answer
When I started programming encapsulation was a difficult concept to swallow. Possibly because I focused on working alone rather than in a team. Covering all the modifiers is too much for this forum. So I'll focus on encapsulation, private and public.
While, this is not the only reason to encapsulate...I can illustrate with an example.
Encapsulation really started to make more sense when I started working in a team. When you are working with another developer you want to divide up tasks. Say you are given the task of creating an Email tool. First, you and the dev2 get together and discuss what you will need:
An Emailer needs data and functionality to work correctly.
data: From, To, Subject, Body, Message, Attachments
functionality: Send(), GetSmtpServer()
The task becomes the class or "object". The data becomes properties. The functionality becomes functions.
public class Emailer
{
public string From
public string To
public string Subject
public string Body
public string Message
public string[] Attachments
public Email(){}
public Send()
{
//functionality
}
private GetSmtpServer()
{
//functionality
}
}
You may ask, "Why did I make GetSmtpServer() private?"
Because the other developer wants to know how to use Email. He doesn't need to know about all my nitty-gritty details. So, he doesn't need to see GetSmtpServer(). This is why I made it private.
Finally, encapsulation groups similar items together into one unit. This makes your code self-documenting. In other words I can look at class called "Emailer" and have a good idea of what it should do. When I see "Send()" I can guess that will send an email.
On the other hand, if I added a method called "CreateInventory()" to Emailer, it really wouldn't be obvious what this function does. No more details on this, besides that this is abuse of encapsulation and another topic.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.