0

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)

Bismuth OP
  • 11
  • 1
  • 1
    As a general piece of advice, if you ever end up on geeksforgeeks.org for any reason, close your browser tab. That site is nothing but empty SEO with no actual useful content, and 90% of the articles I've seen on it have blatantly incorrect information. – Silvio Mayolo May 02 '21 at 06:03
  • @SilvioMayolo ya right ? I feel so bad sometimes because geeksforgeeks just provide definitions and that site is always right on top. Where as some other sites with really good information are really very down. It has just become money making business for them. – Bismuth OP May 02 '21 at 06:07

1 Answers1

0

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.

  • So then why is it said, "we can achieve abstraction only in 2 ways in java i.e Abstract Class and Interfaces" ? According to your answer, it looks like getters and setters as well provide abstraction. Isn't it ? That means there are multiple ways to achieve abstraction in java . Isn't it ? – Bismuth OP May 02 '21 at 07:13
  • @BismuthOP Encapsulation can be achieved in various ways. most computer languages those days(go, C#, javascript) provide functions or rules for OOD and it doesn't have to be class and getter, setter. class is just a manner to achieve Encapsulation, low coupling, etc. – Taehyeong Kim May 02 '21 at 07:31
  • My previous comment was about achieving abstraction not encapsulation ? "In how many ways can i achieve abstraction in java ?" Do you have an answer with proper proof from trusted source ? – Bismuth OP May 02 '21 at 07:36