Is Encapsulation = Data Hiding + Abstraction?

329 views Asked by At

According to geeksforgeeks.org/encapsulation-in-java Encapsulation = Data Hiding + Abstraction ? If yes then I can see data hiding, but where is abstraction here ?

Some people give this as an example of Encapsulation

class Person
{
    
    private String name;
    private int age;
    

    public void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }
    
    public void setAge(int age){
        this.age = age;
    }
    
    public int getAge(){
        return this.age;
    }
}

And some say this is not an example of encapsulation. ref : https://www.youtube.com/watch?v=tjyZWqJkNpc (Also the person teaching in this video is really good in java, it difficult to believe that he can be wrong)

So what is really encapsulation ?

  1. Data Hiding + Abstraction
  2. Grouping variables and methods into a single unit

Which definition is correct ?

(Answer only if you know otherwise ignore this questions and tags o whatever as i am new to stackoverflow and i dont know the rules on how you ask the question. I just want to clear my doubts)

1

There are 1 answers

3
Taehyeong Kim On

Encapsulation can be used for both purposes. In java, encapsulation is achieved by class and class provides access control like private, public. When people code their classes, each programmer can set access rights to prevent inappropriate usage from other class objects. In this way, data hiding is available. And the class users don't have to know the details that even closed to them. Access to the encapsulated information(what member the class has, what other private methods are, etc) is limited and it makes programmers only concentrate on what they should do however complicated the implementation is. So in my opinion, encapsulation can be said to include a notion of abstraction.