Data Binding Data Hiding Encapsulation

Data Binding Data Hiding Encapsulation

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.

Data Binding Data Hiding Encapsulation

Output screen of

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.

Data Binding Data Hiding Encapsulation

Output screen of

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.

Data Binding Data Hiding Encapsulation

Output screen of

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?

33 thoughts on “Data Binding Data Hiding Encapsulation

  1. Sameer Shaikh

    this site is very helpful for me. i have gotten concept of data binding,data hiding,abstraction and encapsulation.
    finally my confusion is released.
    Data Binding =assemble of instance veriable and object of class
    Data Hiding = obj1 dont know information of obj2 and vice versa.
    Encapsulation = 1 variable can be assign multiple value.

    1. S. Nageswara Rao, Corporate Trainer

      One simple example is you are using println() method without knowing the code of println(). That is, the code is abstracted from you. You are using TV without knowing electronics. Just you are using buttons to operate the whole TV without knowing circuits etc. TV mechanism is abstracted from you.

  2. mohit

    hi Sir ,
    here you mentioned that “If an instance variable is called with an object, a location is created in the memory. ” in the first paragraph , my question is isn’t the memory created when we create an object with “new ” keyword.


  3. Ravi

    In my life I have never ever seen such a wonderful deeply and easily explained concept…
    I became your fan sir…. thn u so much sir….

  4. satish

    sir in above program encapsulation concept one object details cannot share another object details
    Officer o1=new Officer();
    Officer o2=new Officer();
    sir tel me o1.sal–>5000 or o1.sal–>address of 5000 in both of them what is true
    then how it reflect to o2.sal=o1.sal; here where o2.sal is getting the value can u plz tel me iam not catching the concept

    1. S. Nageswara Rao, Corporate Trainer

      Using objects Java maintains encapsulation. Books may write many pages on encapsulation, the straight answer is “one variable multiple values”.

      Abstraction varies a little bit. I can give number of examples of my own. In C-lang, you are using printf() function. Without known the internal code of printf(), you are writing to DOS prompt. How it is possible? printf() function code is now shown to you and this we say technically, printf() code is abstracted from you. Similarly, you are riding a motor bike without knowing the piston and accelerator mechanism. We say, the mechanisms are abstracted from the user. To design a auto mobile, we require an auto mobile engineer, but to use, anyone can do. Similarly, TV mechanism is abstracted from you and still you are using.

  5. Mahi

    Why and how Assigning Variable to variable they maintain separate locations,
    and assigning object to obect they refer same location?
    Plz can u gve me proof?

  6. Abdul

    I read many topics on this site and found very very useful while solving any critical problems.

    wanna owe a biga THANK….

    1. S. Nageswara Rao, Corporate Trainer

      One of the programming techniques is declaring variables private and methods public. Other classes can access the private variables through public methods. In public public methods, you can control the input.

      private int birthdate;
      public setBirthDate(int birthdate)
      if(birthdate < 1 || birthdate > 31)
      birtthdate = 0;
      this.birthdate = birthdate;

      If the variable is not private, the other class can assign its own value, which may be wrong.

  7. Muthu

    Your website is very useful for beginners. Concepts are very clear and very easily understandable. Really a awesome website.

Leave a Reply

Your email address will not be published. Required fields are marked *