4

I know there are lots of post regarding this question which has theoretical explanation with real time examples.These OOPs terms are very simple but more confusing for beginners like me. But I am expecting here not a definition and real time example BUT expecting code snippet in java.

Will anyone please give very small code snippet for each one in Java that will help me a lot to understand Encapsulation vs Information Hiding vs Abstraction vs Data Hiding practically?

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
Prashant Shilimkar
  • 7,156
  • 11
  • 45
  • 83
  • 1
    Information hiding is another way to say encapsulation. Giving untrusted (or even trusted) clients access to an object's state in a way that allows clients to maliciously (or accidentally) corrupt an object's state is a very bad thing. To the greatest extent possible, an object's state should be hidden from the world. – scottb Nov 13 '13 at 04:35

2 Answers2

17

Encapsulation = information hiding = data hiding. Information that doesn't need to be known to others in order to perform some task.

class Girl {
  private int age;
  Girl(int age) {
    this.age = age;
  }
  public boolean willGoOutWithGuy(boolean isGuyUgly) {
    return (age >= 22) && (!isGuyUgly);
  }
}

class Guy {
  private Girl girl = new Girl();
  private boolean isUgly = true;
  public boolean willGirlGoOutWithMe() {
    return girl.willGoOutWithGuy(isUgly);
  }
  // Guy doesn't have access to Girl's age. but he can ask her out. 
}

Abstraction = different implementations of the same interface.

public interface Car {
  public void start();
  public void stop();
}

class HotRod implements Car {
  // implement methods
}

class BattleTank implements Car {
  // implement methods
}

class GoCart implements Car {
  // implement methods
}

The implementations are all unique, but can be bound under the Car type.

yamafontes
  • 5,449
  • 1
  • 15
  • 18
  • You mean when we do encapsulation...info and data hiding also comes in picture(code).Please make me correct if am wrong. data and info hiding is way to achieve encapsulation is way to achieve abstraction. Am i right? – Prashant Shilimkar Nov 13 '13 at 04:34
  • 1
    Yes -- all of these concepts tie closely together in OOP. Encapsulation is all about hiding program details from things that don't need to see it. Abstraction is about tying together common behavior among related objects. – yamafontes Nov 13 '13 at 04:41
  • You have given example of abstraction using Car interface.Abstraction is hiding unnecessary info and show important info.Then what we hide and what we show in above example of class.Here i little bit confuse. – Prashant Shilimkar Nov 13 '13 at 04:44
  • The whole point of an `interface` is to provide a universal way of interacting with objects in the `Car` hierarchy. The 3 examples i gave will probably have very different implementations -- but they can still be used as if they were the same type. The idea is to hide the 'under the hood' details of the implementation, while exposing a single API. – yamafontes Nov 13 '13 at 04:50
  • Encapsulation is not about hiding the program details, it's about collecting all the data members and methods operating on those data members in a single container, you can achieve encapsulation without data hiding but you can't achieve data hiding without encapsulation. – raviraja Sep 17 '18 at 11:50
5

To reduce the confusion:

Encapsulation is used for Information hiding or data hiding

Encapsulation means self contained. All the objects in Java have a set of data and methods to operate on that data. So the user of any object does not have to worry about the about how the obect is working. This way you hide the information and other complexities.

Example: Any Java object is enough to represent an example.

Abstraction: This means making things general i.e., instead of creating a very specfic class when you create base classes or interfaces and then extend them to get your specific class.

Example: class Animal {} class Lion extends Animal{}

So here for Lion class you have a generalized class i.e., Animal. This represents abstraction

Note Examples givien by KepaniHaole are perfect.

Abstraction Example:

public interface Animal{
    public String getType();
}

class Lion implements Animal {
    private String animalType = "WILD";

    @Override
    public String getType() {
        return this.animalType;
    }
}
class Cow implements Animal {
    private String animalType = "Domestic";

    @Override
    public String getType() {
        return this.animalType;
    }
}

In this example the Lion and Cow classes implements the Animal interface. The Lion and Cow classes override the getType method of the Animal interface.

Here Lion and Cow are special cases and Animal is more generalized. So this gives you abstraction because whenever you have an Animal you have the getType method to know its type i.e., you have generalized it.

Now if you notice I have made the animalType as private in the Lion and Cow classes so that nobody outside the class can modify it. This way I am hiding unwanted information from outer objects.

All the outer objects need is the getType method to known the type of the animal. This way I am exposing only relavent information to outer objects.

me_digvijay
  • 5,087
  • 5
  • 41
  • 78
  • Please make me correct if am wrong. data and info hiding is way to achieve encapsulation is way to achieve abstraction. Am i right? – Prashant Shilimkar Nov 13 '13 at 04:35
  • 1
    I would say the other ways: When we do encapsulation...info and data hiding also come in picture. So `encapsulation` is a way to achieve this hiding and when you make things general then Abstraction comes into picture. – me_digvijay Nov 13 '13 at 04:43
  • Will you please give very short example of abstraction in java using interface and please explain how we hide not imp and how we show imp info.Thanks – Prashant Shilimkar Nov 13 '13 at 04:47