Saturday, 19 May 2012

How to actually implement Encapsulation

What is encapsulation? Well if you remember the theory it says something like "It is the inclusion within a program object of all the resources needed for the object to function - basically, the methods and the data." Well the above said definition can appear to be very exiting - only to the person who came up with it.

So what is encapsulation? Hide The Data. Yes, it is that simple. Ok, so the next question, how exactly do you hide the data? Simple with the public and private access modifiers. Well the basic rule of encapsulation is that the instance variables should be private and the getters should be public. So the idea of encapsulation is that nobody can set inappropriate values to your instance variables.

For example think of the number of computers that you have, or the number of mobile phones, or the number of girlfriends - all these can be a zero, can't be a negative (Either you have a girlfriend, or girlfriends, or you don't. There cant be a scenario where you have a negative girlfriend!!!). Encapsulation lets you basically set boundaries.

Have you ever had that feeling that comes when you leave your variables exposed? Exposed in the sense as in with the . operator

theDog.height=4

Imagine a scenario of having a direct remote control to change the height of the dog. In the wrong hands that can be pretty bad. So if someone decides to change "theDog.height=0" how would you stop it?

So coming back to the original question, Encapsulation how does it let us set boundaries? That is where the setter method comes in. The setter method can validate the data that is passed and if it thinks it is possible it will pass the values. This is how we can do it.

public void setHeight(int ht)
{
    if(ht>5)
    {
        height=ht;
    }
}
By forcing everyone to call the above setter method we can be sure that there wont be unexpected height problems...


public void setHeight(int ht)
class Saddy 
{
    private int angryLevel;
    // The instance variable is private

    public int getAngryLevel() 
    {
        return angryLevel;
    }
    //get and set methods are public
    public void setAngryLevel(int s) 
    {
        angryLevel = s;
    }
    void situation() 
    {
        if (angryLevel > 60) 
        {
            System.out.println("Run for your life");
        } 
        else if (angryLevel > 25) 
        {
            System.out.println("You have a fighting chance");
        } 
        else 
        {
            System.out.println("Buy an ice cream");
        }
    }
}
The test method in the bottom should make things a bit more clear

class Angertest 
{
    public static void main (String[] args) 
    {
        Saddy  one = new Saddy();
        one.setAngryLevel(70);
        System.out.println("Getting Saddy's Anger Level "+one.getAngryLevel());
        one.situation();
    }
}
Even though the methods don't add functionality, the best part is that you can change your mind later and you can make a method safer, faster and better.

Hope this helps you to understand what encapsulation is all about.

No comments:

Post a Comment