Friday 27 May 2016

Encapsulation

Encapsulation

Encapsulation is the process of binding data member (variable and properties) and member functions (method) into a single unit.
It is very important to understand before start c#. Class is the best example of encapsulation
In simple terms class is binding member, methods into a single unit call class.

Let’s start with c# class example:

using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClass cls = new myClass(1,"Sample",10);

        //Invoice print method
        cls.Print();
    }
}

class myClass
{
    private int _id;
    private string _name;
    private int _age;

    public int Id
    {
        get {
            return _id;
        }
        set {
            _id = value;
        }
    }

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public int Age
    {
        get
        {
            return _age;
        }
        set
        {
            _age = value;
        }
    }

    //default constructor
    public myClass()
    {
        Console.WriteLine("Default Constructor is called...");
    }

    //constructor with parameter
    public myClass(int id,string name, int age):this()
    {
        this.Id = id;
        this.Name = name;
        this.Age = age;
    }

    //Print method to access private member of class;
    public void Print()
    {
        Console.WriteLine("myClass Data:");
        Console.WriteLine("\nID \t Name \t\t Age");
        Console.WriteLine("--------------------------------------");
        Console.WriteLine("{0} \t {1} \t {2}",this._id,this._name,this._age);
        Console.WriteLine("--------------------------------------");
    }
}
Output:
Default constructor is called…
myClass Data:
ID           Name                     Age
------------------------------------------------------
1           Sample                    10
------------------------------------------------------
Press any key to continue…

In above example you can see that we have created myClass which has private fields, properties, constructor and print method.
Using private fields we can access through public properties and also constructor and method of same class.

Let’s try with another example using base and derived class.

using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClass cls = new myClass(1,"Sample",10);

        //Invoice print method
        cls.Print();

        myDerived cls2 = new myDerived(2, "Test", 12);
        
        cls2.Print();

        //myDerived class can access public properties of myClass; 
        //Private can not allow to access.
        myDerived cls3 = new myDerived();
        cls3.Id = 3;
        cls3.Name = "Example";
        cls3.Age = 14;

        cls3.Print();
    }
}

class myClass
{
    private int _id;
    private string _name;
    private int _age;

    public int Id
    {
        get {
            return _id;
        }
        set {
            _id = value;
        }
    }

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public int Age
    {
        get
        {
            return _age;
        }
        set
        {
            _age = value;
        }
    }

    //default constructor
    public myClass()
    {
        Console.WriteLine("Default Constructor is called...");
    }

    //constructor with parameter
    public myClass(int id,string name, int age):this()
    {
        this.Id = id;
        this.Name = name;
        this.Age = age;
    }

    //Print method to access private member of class;
    public virtual void Print()
    {
        Console.WriteLine("myClass Data:");
        Console.WriteLine("\nID \t Name \t\t Age");
        Console.WriteLine("--------------------------------------");
        Console.WriteLine("{0} \t {1} \t {2}", this._id, this._name, this._age);
        Console.WriteLine("--------------------------------------");
    }
    
}
class myDerived : myClass
{ 
    public myDerived()
    {

    }
    public myDerived(int id,string name,int age):base(id,name,age)
    {
        base.Id = id;
        base.Name = name;
        base.Age = age;
    }
    //Print method to access private member of class;
    public override void Print()
    {
        Console.WriteLine("myClass Data:");
        Console.WriteLine("\nID \t Name \t\t Age");
        Console.WriteLine("--------------------------------------");
        Console.WriteLine("{0} \t {1} \t\t {2}", base.Id, base.Name, base.Age);
        Console.WriteLine("--------------------------------------");
    }
}
In the above example you can see that we have created two classes, one is the base of another derived class. It has virtual method which is override in derived class to print the base class member properties.
Also you can access public fields, properties, methods of base class using instance of derived class. You can see we have create instance of myDerivide class as “cls3” and we have accessed “cls3.Id”,”cls3.Name”, “cls.Age” and then print.
  • Encapsulation protects abstraction.
  • Flexibility and extensibility of the code.
  • Reduction in complexity.
  • Specification of the accessibility of each of the members of a class to the code outside the class.
  • Encapsulation of class can hide the internal details.

you can also visit:  www.mybook-lang.com

No comments:

Post a Comment