Click here to Skip to main content
15,891,633 members
Articles / Programming Languages / C#
Tip/Trick

HelloWorld - C# OOP Encapsulation

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Sep 2013CPOL3 min read 17K   7   2
My take on helping Beginners audience to help them understand complex world of software development

Introduction

Today I am introducing a "HelloWorld" a series of small write up talking about information technology, mainly with emphasis on the world of software development. There are so much cool stuff available on net talking about the same, however, I find in comparison majority of target audience is rather intermediary to advance level. That leaves the beginner level folks with a prayer, “they will figure it out anyway”. So, here is my take to help them, my target audience falls in “spoon feed me NOW!” category. For those who don’t belong to this category, salute goes out to your courage, please continue to read, you never know!

So, what am I going to talk about? Well, I can talk about OOP Encapsulation or new HTML5 <canvas> or Multithreading or abracadabra or anything in and everything in between. Having said “Encapsulation”, why not, let’s start with this one! Before I begin, I do assume you have some association with the world of software development, if not, then good luck! Should you stop reading? Hell no, you are already in the zone, one day, one day it will help you, trust me, knowledge never goes to waste.

Now, let’s get to the exciting part, if you heard of object oriented programming (OOP) you are no stranger to “Encapsulation”, however, how many if you actually know what the heck is Encapsulation anyway? Simply put, it means “Hide It”. OK. Hide what, and from whom? In the world of OPP, we have classes, and they are type of domain, in the business world the examples would be: customer, vendor, inventory, orders and shipping etc. So, if your project has two classes Inventory and Shipping, there inner details must be hidden from each other. Let me show you this with the help of the following diagram: 

Image 1 

As you can see the diagram, Inventory class has field member “catalogid”, the implementation of this data field is only visible to the properties and methods belong to the class, same goes with field member “orderid’ in shipping class. We usually create interfaces to let classes talk to each other, in this example, if shipping class needs to know the “catalogid” data, it will send a request through interface and get the response back through the interface. What is Interface? Well, slow down, will take a dig at that too soon.

C#
public class Inventory
{
    int catalogid;

    //setter and getter
    public int CatalogId
    {
        set
        {
            catalogid = value;
        }
        get
        {
            return catalogid;
        }
    }

    //new short cut way
    public int OneMoreCatalogId { get; set; }
}

If you notice the simple C# code implementation of the Inventory class, you will see the field member “catalogid” of int data type declared as private. If you create the instance object of this class, you will not able to refer to this field, because it is encapsulated within the class boundary, that is what “Hide It!”  That’s how it should be, if you need to access “catalogid” data either for read or write, you will make use of property CatalogId or “OneMoreCatalogId” both are same, just a different form of implementation. You can also code methods to manipulate the field. The short cut method shown in the code is same as the other one, only magic here is the complier helps you to convert the short cut implementation to regular getter setter form. The keyword “value” there is nothing but hidden parameter passed to getter setter. 

So encapsulation helps us here to hide the complexity and inner working of the given class to other classes in the problem domain. In our example, orders will come to Inventory class as request with the help of interface and a desired response will he return to Shipping class again with the help of interface. Another point to take home is the “division of responsibilities”, can you image one class doing everything from taking orders to managing inventory to shipping and billing etc.? that would be nightmare right? 

So the conclusion is by using proper Encapsulation when we design our classes, we can keep things nice clean and responsible.  Let me know what you think, I hope to keep the language as simple as possible, however, if you think “Spoon feed me now and chew it too?” what can I say, on serious note, hope it helps J 

Comments are always welcome! 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect FeatherSoft Inc.
Canada Canada
Asif Sayed has over twenty + years experience in software development and business process architecture. He has a consulting firm in Toronto, Canada. His firm provides IT solutions to all sizes of industries. He also teaches .NET technologies at Centennial College in Scarborough, Ontario. Recently he has become member of team as a subject matter experts with Microsoft's Learning Division. He has a book published by Apress with the Title "Client-Side Reporting with Visual Studio in C#".

My blog: http://www.dotnetsme.com
My Website: http://www.feathersoft.ca

Comments and Discussions

 
Questionc# oop encapsulation Pin
n00bcode10-Mar-16 11:51
n00bcode10-Mar-16 11:51 
QuestionCan you explain Abstract? Pin
Rajesh Kumar Chekuri14-Sep-13 8:56
Rajesh Kumar Chekuri14-Sep-13 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.