Home > DeveloperSection > Blogs > Information Hiding in Object Oriented Programming (OOPS)

Information Hiding in Object Oriented Programming (OOPS)


Java Java  OOPS  Software Development 
Ratings:
0 Comment(s)
 208  View(s)
Rate this:

Information Hiding in Object Oriented Programming (OOPS)


In our everyday life, many of us oftentimes try to hide information from other people. In many situations, the information hiding is an essential part of our life. For example, when we go grocery shopping, do we allow the store clerk to withdraw money from our wallet? The wallet (which is an object in object-oriented terms) hides money (which is the information or attribute/field of an object) from outsiders. A wallet may provide a method called “pull out X dollars” that, when executed by an outside object, results in handing over X dollars to the caller without disclosing how much money is in it. This is called information hiding in object-oriented programming.


Let’s now put these concepts to work via a practical example. The program implemented here has classes for the wallet and a person who pulls money out of a wallet object.

Wallet Class:

class Wallet

{

          private float money;

 

          public void setMoney(float money) {

                   this.money = money;

          }

 

          public boolean pullOutMoney(float amount) {

                   if (money >= amount) {

                             money -= amount;

                             return true;

                   }

                   return false;

          }

}

 

Person Class:

 

class Person

{

          public static void main(String[] args)

             {

                   Wallet wallet = new Wallet();

                   System.out.println("Putting $500 in the wallet\n");

                   wallet.setMoney(500);

 

                   System.out.println("Pulling out $100 ...");

                   boolean isMoneyInWallet = wallet.pullOutMoney(100);

                   if (isMoneyInWallet) {

                             System.out.println("Got it!");

                   } else {

                             System.out.println("Nope, not enough money");

                   }

 

                   System.out.println("\nPulling out $300 ...");

                   isMoneyInWallet = wallet.pullOutMoney(300);

                   if (isMoneyInWallet) {

                             System.out.println("Got it!");

                   } else {

                             System.out.println("Nope, not enough money");

                   }

 

                   System.out.println("\nPulling out $200 ...");

                   isMoneyInWallet = wallet.pullOutMoney(200);

                   if (isMoneyInWallet) {

                             System.out.println("Got it!");

                   } else {

                             System.out.println("Nope, not enough money");

                   }

          }

}

 

When we compile and run this program, we see the following output:

Putting $500 in the wallet

Pulling out $100 ...

Got it!

Pulling out $300 ...

Got it!

Pulling out $200 ...

Nope, not enough money

 

 

 

Public and private access modifier:

The Wallet class declares one field called money, as you would expect it to do. Naturally, the type of this field is set to float so that we can keep our small change in the wallet:


class Wallet {

private float money;


Notice the private keyword in front of this field declaration. This is the access modifier, this access modifier controls the visibility of the declaration to which it is attached. In this case, it is attached to a field declaration. We can also apply access modifiers to class and method declarations. 


One such method we have used so far is the main method in our earlier programs. This main method has a public modifier attached to it. The main method is declared public so that it can be invoked by the JVM—or to be more precise, by an external object. It is mandatory for the main method to be declared public. We also apply the access modifiers to the class declarations.


Setter-Getters Methods to access private fields:

The wallet class also defines a method called setMoney, as follows:

public void setMoney(float money) {

this.money = money;

}

•The method takes a float argument and does not return a value to the caller. 


•The method is declared public and therefore can be invoked by the code in an external object (obviously, we want to load the wallet with some money when we go shopping). 


•Inside the method’s body, we copy the value of money received as the method parameter to the instance variable with the same name, money.


•Because the method parameter and the instance variable use the same name, we need to differentiate between them in our code. This is done with the help of the this keyword. 


•The this keyword is a reference to the current object, and thus the syntax “this.” A dot following the object reference refers to some variable or a method within the object.


•The syntax this.money refers to the money field of the current object. We set this field to the value passed in the method parameter.


•This method helps in loading our wallet with some money. Incidentally, this method is called a setter or a mutator method because it sets the value of a field. 


Setter methods, by convention, start with the word set, followed by the name of the field on which they operate, with the first letter of the field capitalized. Similar to the setter methods, there are getters methods that start with the word get and return the value of a field to the caller. 


Now the question is, why do we need to apply access modifiers to our declarations? So far, we have seen two access modifiers—private and public. The private modifier makes the corresponding field, method, or class declaration truly private to the code to which it belongs.


In our example, the variable money is declared private and is therefore visible only to the code within the Wallet class. Therefore, using the dot syntax (wallet.money) will result in a compilation error. The only way to access the money field is to use the public getter/setter methods that operate on this attribute. Making money private also helps in protecting it from any accidental modifications by an external code because one cannot use the objectReference. fieldName syntax to access it.


Don't want to miss updates? Please click the below button!

Follow MindStick