The BASE for JAVA LEARNING

by PRO FE S S OR


INFORMATION HIDING

AN OBJECT-ORIENTED FEATURE


Information Hiding goes hand by hand with Encapsulation as it is implemented within a class by making its attributes and methods not visible from outside its boundary.
  • When members (attributes and methods) of a class are private, they are not visible by any other Object of a different class. They are only accessible to their class.


  • The norm in Object-Oriented Programming is to have as many attributes and methods private as possible.

  • Why INFORMATION HIDING:

  • Security, avoiding unnecessary code tampering.


  • De-coupling. In Object-Oriented Programming high de-coupling is desirable.
    Information Hiding aids in keeping high de-coupling among objects, as private members of a class are not visible and therefore can not be used to communicate among objects.

  • JAVA ways of implementing Information Hiding:

    By the use of the 'private' modifier in front of the member.

    Example of JAVA class showing Information Hiding:

    class Treasure
    {

    private double amount;
    private int codedNumber ;
    Treasure(double tmpMoney)
    {
    amount = tmpMoney;
    }
    private void calculate()
    {
    amount*= codedNumber;
    }
    }

    // All members of above class are made private in this example.