Data Binding Data Hiding Encapsulation

April 9, 2011 | oops concepts

Data Binding Data Hiding Encapsulation

Summary: This tutorial "Data Binding Data Hiding Encapsulation "is targeted as a good starting point for those who are new to OOPS concepts. Even if you are familiar with, start reading and you may find something useful that you have not known earlier. Or you can enjoy the approach I adopted to clear the confusing concepts of OOPS.

To go before into the subject, just read the program twice. You may guess some values how they are printed.

public class Officer
{
  int salary;
  public static void main(String args[])
  {
    Officer o1 = new Officer();
    Officer o2 = new Officer();

    o1.salary = 5000;
    o2.salary = 6000;

    System.out.println("Salary of o1 before initially Rs." + o1.salary);// 5000
    System.out.println("Salary of o2 before initially Rs." + o2.salary);// 6000

    o1.salary = 7000;
    System.out.println("Salary of o1 after changing o1 Rs.:" + o1.salary);// 7000
    System.out.println("Salary of o2 after changing o1 Rs." + o2.salary);// 6000

    o2.salary = 8000;
    System.out.println("Salary of o1 after changing o2 Rs.:" + o1.salary);// 7000
    System.out.println("Salary of o2 after changing o2 Rs." + o2.salary);// 8000
  }
}

[caption id="attachment_337" align="aligncenter" width="513" caption="Output screen of Officer.java"]Data Binding Data Hiding Encapsulation[/caption]

Data Binding Data Hiding Encapsulation

salary is an instance variable and should be accessed with an object only, through static main() method. Two objects o1 and o2 are created and salary field is called. The object o1 gets a salary of 5000 and o2 gets 6000. If an instance variable is called with an object, a location is created in the memory. Now two locations are created in the memory for o1.salary and o2.salary. That is, one location of salary is binded with o1 and another location of salary is binded with o2. This is is known as data binding. Due to this binding habit, the salary location of o1 is hidden from o2 and similarly the salary location of o2 is hidden from o1. This is known as data hiding. Due to hiding, o1 cannot access o2 salary and o2 cannot access o1 salary. That is, the variable salary is encapsulated with object o1 and becomes one unit of data. Similarly another unit o2 is made with its own salary, entirely separate from o1 unit. This is known as encapsulation. Encapsulation is a pharmaceutical term where drug powder is placed in a capsule to hide its properties, like taste and color etc., from the patients. Due to data hiding, the properties (the value of fields(instance variables)) of an object are hidden from other objects. This exactly resembles a capsule. The advantage of encapsulation is one instance variable can be given multiple values, of course, by calling with different objects. Encapsulation is an object oriented concept or paradigm or design or principle; you may say anything, all amounts the same.

In the later part of the program, salary of o1 is changed. Only the salary of o1 is affected but not o2 and when o2 salary is changed, o1 salary is not affected. It is due the concept of encapsulation. This is where object-oriented programming takes a lead over procedure-oriented programming, like C. Finally, data binding leads to data hiding and data hiding leads to encapsulation.

Encapsulation – Want or Don’t Want

Now let us see how to assign the properties of one object to another by modifying the previous Officer program.

public class Officer1
{
  int salary;
  public static void main(String args[])
  {
    Officer1 o1 = new Officer1();
    Officer1 o2 = new Officer1();
    Officer1 o3 = new Officer1();
    Officer1 o4 = new Officer1();
    o1.salary = 5000;
    o2.salary = o1.salary;                        // assigning variable to variable
    System.out.println("o1 initial salary Rs." + o1.salary);    // 5000
    System.out.println("o2 initial salary Rs." + o2.salary);   // 5000

    o1.salary = 6000;
    System.out.println("o1 salary after changing o1 Rs." + o1.salary); // 6000
    System.out.println("o2 salary after changing o1 Rs." + o2.salary); // 5000

    o3.salary = 7000;
    o4 = o3;                                      // assigning object to object
    System.out.println("o3 initial salary Rs." + o3.salary);  // 7000
    System.out.println("o4 inital salary Rs." + o4.salary);  // 7000
 
    o3.salary = 8000;
    System.out.println("o3 salary after changing o3 Rs." + o3.salary);  // 8000
    System.out.println("o4 salary after changing o3 Rs." + o4.salary);  // 8000
  }
}

[caption id="attachment_338" align="aligncenter" width="513" caption="Output screen of Officer1.java"]Data Binding Data Hiding Encapsulation[/caption]

The basic concept in the above code is to observe encapsulation by assigning variable to variable and object to object. Four objects are created. o1 salary is set to 5000 and later o1 salary is assigned to o2 salary. They maintain separate locations and advantage is if o1 salary is changed to 6000, o2 salary is not affected. Observe the output. Later o3 salary is set to 8000. Object o4 is assigned with o3. In Java, when object to object is assigned, they maintain or refer the same location. That is, o3 and o4 refer or point to one salary only. The affect is if one object o3 changes the salary, the other object o4 also changes. Observe the output. Similarly, you can change o4 salary and notice o3 salary also changes; no encapsulation. Copying concepts are more discussed in Copying an Object – Shallow Copying Vs. Deep Copying.

Final conclusion, we can make out is, if variable to variable is assigned perfect encapsulation is maintained and object to object is assigned no encapsulation is maintained. This must be kept in mind while assigning properties of one object to another. Another style of copying is cloning which we get later.

Using Thin main() Method

In the previous, Officer1 program, all the code is written in the main() method only. You may get output but it is against coding principles. Coding principles say, keep the main() method as thin as possible by placing less code. Limit the main() method just to create objects and calling the methods. Write any code in a method and call the method from the main(). This increases the reusability, readability and debugging becomes easier. Let us modify the previous program.

public class Officer2
{
  int salary;
  public void display(int sal)
  {
    salary = sal;
  }
  public static void main(String args[])
  {
    Officer2 o1 = new Officer2();
    Officer2 o2 = new Officer2();

    o1.display(5000);
    o2.display(6000);

    System.out.println(o1.salary);                               // 5000
    System.out.println(o2.salary);                               // 6000
  }
}

[caption id="attachment_340" align="aligncenter" width="513" caption="Output screen of Officer2.java"]Data Binding Data Hiding Encapsulation[/caption]

The difference between Officer1 and Officer2 programs is assigning the values to objects o1 and o2. sal is a local variable to display() method and salary is an instance variable (the other name of global variable). Local variable sal is assigned to instance variable salary. When o1.display(5000) is called, 5000 goes to sal and in turn sal gives to salary. Because salary is an instance variable, one location on the name o1.salary is created and is accessed though main(). But o1 is not linked to sal because sal is a local variable and thereby no location is created. Similar discussion is available in Using Variables from Methods.

Next program is a modification this program where "this" keyword is used.

How to explain the difference between Encapsulation and Abstraction before interviewer?