Learn Object Oriented Programming Using C#: Part 5

Before reading this article, please go through the following articles:

  1. Learn Object Oriented Programming Using C#: Part 1

  2. Learn Object Oriented Programming Using C#: Part 2

  3. Learn Object Oriented Programming Using C#: Part 3

  4. Learn Object Oriented Programming Using C#: Part 4


Dear readers, today we will discuss the first pillar of Object Oriented Programming systems. Please read the article Learn OOP Using C# Part 3 in which we have already discussed classes and properties in detail. In fact we have used the concept of encapsulation in that article without using the word encapsulation. "Encapsulation is a process of binding data members (variables and properties) and member functions (methods) into a single unit". And Class is the best example of encapsulation.

Here is a very simple diagram that will explain encapsulation itself:


Key Points of Encapsulation

  • Protection of data from accidental corruption
  • Specification of the accessibility of each of the members of a class to the code outside the class
  • Flexibility and extensibility of the code and reduction in complexity
  • Encapsulation of a class can hide the internal details of how an object does something
  • Using encapsulation, a class can change the internal implementation without affecting the overall functionality of the system
  • Encapsulation protects abstraction

In very simple words we can say that encapsulation is a technique to hide the complexity of a class (its member variables and methods) from the user. It makes it easier for the user to use only the accessible required methods plus it will protect the data from accidental corruption.


using System;

namespace Encapsulation


    class Program


        static void Main(string[] args)


            credit_Card_Info cr = new credit_Card_Info("Naveed Zaman", "002020-1",5000,16.5);





        class credit_Card_Info


            private string customername=string.Empty  ;

            private string cardno = string.Empty;

            private double creditamount=-1;

            private double markuprate = -1;


            public credit_Card_Info(string _customername, string _cardno, double _creditamount, double _markuprate)


                customername = _customername;

                cardno = _cardno;

                creditamount = _creditamount;

                markuprate = _markuprate;



            public double disbursement




                    return creditamount;




                    creditamount = creditamount + value;

                    creditamount = creditamount * markuprate / 100 + creditamount;





             public void display()


                Console.WriteLine("Customer Name :{0}", customername);

                Console.WriteLine("Card Number :{0}", cardno);

                Console.WriteLine("Markup Rate :{0}", markuprate);

                Console.WriteLine("Current Balance with Markup:{0}", creditamount);








In that example we have created the class credit_Card_Info with four member variables having private access modifiers. Then we have defined the constructor of the class that initializes the four member variables of a class. After that we used get and set properties of the class to access the private variable of a class. The last display method will show the output of the class member variables.

So we can observe that the user must provide only the "cr.disbursement=6999" amount and encapsulation will calculate the markup and outstanding of the client. In that simple example the user did know the complexity of the class member variables and methods.

Another very common example of encapsulation is TextBox.

For Example:

TextBox tb = new TextBox();
tb.Text ="Hello World";