0

By definition, encapsulation in Java is a process of wrapping code and data together into a single unit. But if a class has 2 member variables and a method and both the variables and method has public access modifier, can we say that class as encapsulated class?

For example, can we say below class is encapsulated or not

public class AddNumbers {

     public int a;
     public int b;

     public void add(){
          System.out.println(a+b);
     }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Your code is not syntactically valid. Usually that's not a big issue if the code is purely for demonstrating some general structure, but the fact that you define two arguments to the `add` method with the same name as the fields make this very suspicious. What did you *intend* the `add` method to do: take two arguments and add them together or return the sum of the two fields? In either way, I'd not describe this as "encapsulated". – Joachim Sauer Dec 12 '22 at 11:21
  • Thanks for the quick response @JoachimSauer. I have modified the class. Can you please answer the question now. I am seeking the reason behind why we cannot say it as encapsulated class. – akshay lonkar Dec 12 '22 at 11:30
  • 1
    I don't think your first sentence is correct. What you're describing is more generally object-oriented programming. Encapsulation is restricting access to something (usually internal state). No access is restricted in your sample code: everything (most importantly the fields, i.e. the state of the object) is fully (publicly) accessible to any other code. – Joachim Sauer Dec 12 '22 at 11:32
  • Does this answer your question? [What are the differences between information hiding and encapsulation?](https://stackoverflow.com/questions/13913174/what-are-the-differences-between-information-hiding-and-encapsulation) – jaco0646 Dec 12 '22 at 15:47
  • 1
    Hi @jaco0646, even the link you shared answered my question. Thanks – akshay lonkar Dec 12 '22 at 17:10

2 Answers2

0

Yes, or no. Depending on how one defines "encapsulation".

Given the phrasing of your question, I assume you are working with the definition of encapsulation from Wikipedia, which (as of now) reads

In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on those data, or the restricting of direct access to some of an object's components.

I disagree with this definition. Or more precisely, I think it unnecessarily distinguishes "encapsulation" and "data hiding" which is not in line with how I see the term "encapsulation" used in day-to-day conversations. In fact even the Wikipedia talk page mentions this problem of the definition (with no apparent counter-voices).

So if you follow that Wikipedia definition, then yes, your code uses encapsulation ("an encapsulated class" is not a phrase I've ever heard anyone use this way, I'd avoid it).

If you follow the school of thought that information hiding and encapsulation are synonymous (or at least very tightly bound together) then your code is not using encapsulation.

See this paragraph from the Wikipedia article on Information Hiding:

The term encapsulation is often used interchangeably with information hiding. Not all agree on the distinctions between the two, though; one may think of information hiding as being the principle and encapsulation being the technique.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users

to make your class encapsulated you need

  • Declare class variables/attributes as private
  • provide public get and set methods to access and update the value of a private variable

your class has public data, that is, it is not protected (encapsulated) and therefore can be accessed and modified by everyone I recommend this reading for a better understanding of encapsulation

java_encapsulation

  • 2
    "*provide public get and set methods to access and update the value of a private variable*" I strongly disagree that this constitutes encapsulation. The data is exposed to clients of that class, just via 1 level of indirection. – Michael Dec 12 '22 at 12:09