Practical example Encapsulation vs Information Hiding vs Abstraction vs Data Hiding in Java

22,839

Solution 1

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

class Bartender {
  private boolean hasBeer = false;
  public boolean willGiveBeerToDrinker(int drinkerAge) {
    return (hasBeer && (drinkerAge >= 21));
  }
}

class Drinker {
  private Bartender bartender = new Bartender();
  private int age = 18;
  public boolean willBartenderGiveMeBeer() {
    return bartender.willGiveBeerToDrinker(age);
  }
  // Drinker doesn't know how much beer Bartender has
}

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.

Solution 2

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.

Share:
22,839
Prashant Shilimkar
Author by

Prashant Shilimkar

Updated on November 21, 2021

Comments

  • Prashant Shilimkar
    Prashant Shilimkar over 2 years

    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?

  • Prashant Shilimkar
    Prashant Shilimkar over 10 years
    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
    Prashant Shilimkar over 10 years
    Please make me correct if am wrong. data and info hiding is way to achieve encapsulation is way to achieve abstraction. Am i right?
  • yamafontes
    yamafontes over 10 years
    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.
  • me_digvijay
    me_digvijay over 10 years
    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.
  • Prashant Shilimkar
    Prashant Shilimkar over 10 years
    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
    Prashant Shilimkar over 10 years
    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
  • yamafontes
    yamafontes over 10 years
    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.
  • raviraja
    raviraja almost 6 years
    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.