Click here to Skip to main content
14,699,759 members
Please Sign up or sign in to vote.
3.40/5 (5 votes)
See more:
I problem with concept of Encapsulation and implement.

Please guide me!!!
Posted
Updated 22-Jan-11 9:58am
v2
Comments
Not Active 22-Jan-11 14:11pm
   
I believe you mean encapsulation, not capsulation. What problem do you have with it? It is difficult to answer such a generic, non-specific question
Sandeep Mewara 22-Jan-11 14:28pm
   
Can you elaborate please.

1 solution

Encapsulation means: We must put the details of the objects into the objects. It's benefits are:

1. We Don't encounter with details of objects.
2. It's easy to control some changes.

For Example: Vehicle Class.

You have 2 properties. (1. AutoAccelerator 2. Speed)

You can not increase the speed directly but you can push on the gas and then the speed will be increased.

public class Vehicle
{
    private int _AutoAccelerator;
    public int AutoAccelerator
    {
        get
        {
         return _AutoAccelerator;
        }
        set
        {
            _AutoAccelerator = value;

            if(_AutoAccelerator > 10)
                _AutoAccelerator =10;
            if (AutoAccelerator < 0)
                AutoAccelerator = 0;
            Speed = _AutoAccelerator * 20;
        }
    }

    private int _Speed;
    public int Speed
    {
        get
        {
            return _Speed;
        }
        private set
        {
            _Speed = value;
        }
    }
}


Encapsulation can help you that your developers wont encounter with details. You can also change computing speed formula easier than hardcode.

Good Luck
   
v2

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900