Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top