Encapsulation


Puppy cannot take the cookie..  But you can.. So probably puppy needs you to take it…

Well.. In Java also we need to keep things where everyone cannot access (Others can access only via authorized people). We call it “Encapsulation”.

Encapsulation is one of the Object Oriented principles. Keeping the fields of a class private and giving the access of those private fields only via public methods is the technique of encapsulation.

So when there’s a private variable in a class you need to either set a value for the variable or get value of the variable. It’s true that you can use any name for the methods. But the standard way is,
If field is,

private int age;

To set a value for the field :

public void setAge(int newAge){
   age = newAge;
}

To get the value of field :

public int getAge(){
   return age;
}

It is called accessing the variable using getters and setters. See when the variable name is “age” how the method names are created using “set” and “get” prefixes. So we don’t directly ask the age… Instead use someone else to ask.. 😉 Cool and safe right…?

There are many advantages of using encapsulation in your java code.

  1. The field is visible only to the class it belongs.
  2. It makes easy to maintain the code. When many classes use the same field of a class it can be used without effecting on other classes.
  3. The class that owns the private field has the full control over the field; changing it doesn’t violate the other classes.

Here is an example where encapsulation is used. We are using two classes (Student.java and RegisterStudent.java) for the example. Student class contains the encapsulated variable and RegisterStudent uses it.. Let’s see how to do.

Student.java

public class Student{
  private String studentName;

  public String getStudentName(){
     return studentName;
  }

  public void setStudent ( String name){
     studentName = name;
  }
}

RegisterStudent.java

public class RegisterStudent{

   public static void main(String args[]){
     Student student= new Student ();
     student. setStudent ("Sheldon");
     student. getStudentName ();

     System.out.print("Student Name : " + student. getStudentName ());
   }
}

In Student class there’s a private variable studentName. To access it from RegisterStudent class we have used a getter and setter. getStudentName will return the value of studentName and setStudentName sets the value of its parameter to the studentName.

After that in RegisterStudent class first we have created a Student object call student. Then set a value to the studentName variable in Student class using the setter. Finally print its value, using getter.

Here’s the output.. It prints “Student Name : Sheldon

So…… Get set.. Ready…Go…………. Try yourself… Encapsulation is more important than you think….

So.. What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s