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?

1
  • 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, 2013 at 4:35

2 Answers 2

17

Encapsulation = data hiding. Concealing information that doesn't need to be revealed to others in order to perform a 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.

5
  • 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? Nov 13, 2013 at 4: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.
    – sandymatt
    Nov 13, 2013 at 4: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. Nov 13, 2013 at 4: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.
    – sandymatt
    Nov 13, 2013 at 4: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, 2018 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.

3
  • Please make me correct if am wrong. data and info hiding is way to achieve encapsulation is way to achieve abstraction. Am i right? Nov 13, 2013 at 4: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. Nov 13, 2013 at 4: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 Nov 13, 2013 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.