1

I am very new to programming. Can anyone explain why data hiding is done in any oop language? If the client cannot view/change my code why do I need to hide data. I'll never display or use that data. What warrants hiding?

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
aakash
  • 13
  • 3
  • 2
    Can you post an example of what you mean? – Luiggi Mendoza Sep 20 '13 at 17:23
  • 3
    One of reasons: [why-use-getters-and-setters](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – Pshemo Sep 20 '13 at 17:24
  • How the client cannot view/change your code while it's not hidden? – cpp Sep 20 '13 at 17:28
  • For details on encapsulation (and a lot more): http://www.amazon.com/Effective-Specific-Improve-Programs-ebook/dp/B004V4420U/ref=pd_sim_kstore_8 – Zac Howland Sep 20 '13 at 17:31
  • 1
    You should ask this on programmers.stackexchange.com – Nemanja Boric Sep 20 '13 at 17:33
  • you only **think** the client cant change your code. – Woot4Moo Sep 20 '13 at 17:35
  • It is not necessary for your client to view/change your code in order to be able to mess up with your data. – Nemanja Boric Sep 20 '13 at 17:36
  • when I give the client a .jar/.exe can he still change my code?@cpp – aakash Sep 20 '13 at 17:36
  • You are probably talking about `encapsulation` (one of three component of OOP). Class internals need to be hidden so you can change your internal logic, but other classes that use yours will not have no need to touch their code. So it's hiding from other classes, not users. – PM 77-1 Sep 20 '13 at 17:36
  • 2
    Oh, and when they say *client* they usually don't mean the user behind the keyboard, but the other classes that use your class. – Nemanja Boric Sep 20 '13 at 17:36

2 Answers2

10

It is about complexity. Take your car's engine for example. It is a very complex object. You can, if you know enough get in there and twiddle with stuff and operate the car. However, this would be very dangerous. You can maybe do things that the engine designer did not intend.

In this crazy scenario you are given an interface. That is the driver's position with the steering wheel, gears, pedals etc. In that position the things you can do are restricted, and safe, and driving the car is easy. For instance, you cannot change the gear out of park without first stepping on the brake pedal. If I bypass the interface and go directly into the engine I would probably be able to do anything even if it leads to the destruction of the engine.

To apply this to software consider if you have a Fraction class that is made up of 2 integer values: a numerator and a denominator. There is a class invariant ( a rule) that says: denominator cannot be 0. If the denominator did happen to be 0 then the Fraction object will make no sense and will be useless. In the following code I am not hiding the variables:

   public class Fraction{
         int numerator;
         int denominator;

         public Fraction add(Fraction f){
             //add this Fraction to f
         }

         ....
   }

In this case the client code can do this:

    Fraction f1 = new Fraction();
    f1.numerator = 2;
    f1.denominator = 0;

    Fraction f2 = new Fraction();
    f2.numerator = 3;
    f2.denominator = 4;

    Fraction f3 = f1.add(f2);

What does your add method do here? What this code does is give the responsibility of ensuring that such problems are avoided to the client. With proper encapsulation the responsibility of ensuring that all objects are constructed with integrity belongs to the class itself.

  public class Fraction{
       private int numerator;
       private int denominator;

       public void setDenominator(int d) throws IllegalArgumentException{
             if(d == 0){
                 throw new IllegalArgumentExcepton("Denominator cannot be 0");
             }
             this.denominator = d;
       }
      ...
  }

If the client tries to do this:

    Fraction f1 = new Fraction();
    f1.setDenominator(0); ///exception

And everyone is safe.

So to summarize:

  • Makes your objects safe - client cannot do things that you did not intend
  • Makes your objects easier to use - client does not need to know all the inner workings of the class to use it
  • contributes to the reusability of your code - if you decide to change the implementation of the class, as long as you do not change the interface then the clients are not affected.
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
1

This seems like a pretty broad question covering wides swaths of consideration but I'll try to address a couple items:

Sure your client may not be able to see or change your code (if you provide a complete compiled application) but you and any other maintainers can. The purpose of data hiding is to minimize the points-of-interaction between data and each level of an interface. This helps you to write and maintain correct code.

For the same reason that global variables can be extremely hard to use and maintain in a correct way, the more you localize use of data the easier it is to grok the code and be confident of correctness.

When you provide an abstract interface to a class you allow the class to operate on its internal data/state without the outside world needing to know anything about the underlying data structure, types, or algorithms used. This then makes your code much simpler for clients to use effectively.

Mark B
  • 95,107
  • 10
  • 109
  • 188