Thursday 21 April 2016

OOP conecpt in c# part3



Encapsulation:
Encapsulation is important part of Object Oriented Programming language. It makes “OOP” safer and easy to use.
Encapsulation means wrapping of Data Member and Method together into a single unit (i.e. class).
Encapsulation hides internal details of state and behavior of object. We can also say that it the process of hiding all internal details of an object from outside the world. It (Encapsulation) is the ability to hide its data and member from outside world and show only necessary part i.e. data and member to outside world.

Example:-

class BankAccount
{
    public void CloseAccount
    {
        ApplyPenalty();
        CalculateFinalInterest();
        DeleteAccount();
    }

    protected  virtual void ApplyPenalty()
    {
        // your code
    }
    protected  virtual void CalculateFinalInterest()
    {
        // your code
    }
    protected  virtual void DeleteAccount()
    {
        // your code
    }
}

When user (programmer) wants to close an account then they shouldn’t worry about the various step to follow below steps:-
      1.)  Check Penalty
      2.)  CalculateFinalInterest
      3.)  Close Account

Programmer just calls CloseAccount method and all necessary steps will be taken. So that CloseAccount method is public and other are protected. 

RealTime Example :-

TV
It is encapsulated with cover and we can operate TV with remote. So no need to Remove cover and change the channel. Hence everything is private except remote so that anyone can access TV through remote.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.