Java:OOPs concept – Data hiding

Data hiding:

The data hiding provides data integrity and data security and the other class member or method cannot access them directly our class methods if we can to access we can provide the public methods.

Providing class data members not access to other class is called Data hiding. In Java, we can achieve data hiding by defining data member as private.

Example :

 

       class DataHidingDemo {
          private int x;
          
          public void setX(int x) {
            this.x = x;
         }
         public int getX {
            return x;
         }
       }

 

In data hiding, we define the data member i.e class variable with the private modifier, here we can set the data member data with setter and getters, So maintainability or managing or manipulating the data will become very easy.

Data hiding is one Object Orientated Programming feature, so it highly recommended to declare data member as private.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s