3

Here is a class Encapsulate which has public property and a public method.

class Encapsulate
{
  public int a;
  public int b;

  public static void main(String...s)
  { 
    Encapsulate e = new Encapsulate();
    e.setVar(10,20);
    System.out.println(e.getSome());
  }

  public void setVar(int a, int b)
  {
    this.a = a;
    this.b = b;
  }

  public int getSome()
  {
    return a + b;
  }
}

Is the OOP concept Encapsulation followed here?

I am sure that it is applicable as both are bonded together in a class, may be we can call it weak encapsulation but there is encapsulation.

But I don't know how to prove it because my searches showed that class properties should be private.

Update : What in case of default access specifier.

Himanshu Bhandari
  • 1,769
  • 2
  • 23
  • 37
  • I don't know, if this is really correct, but usually I say that *data hiding* and *encapsulation* are related, but not the same. – meskobalazs Feb 27 '16 at 09:48
  • This might be interesting: http://programmers.stackexchange.com/questions/173547/what-is-the-difference-between-data-hiding-and-encapsulation – meskobalazs Feb 27 '16 at 09:52
  • If I am binding property and methods using public access specifier, than also they are hidden from other classes and bounded together(That's what encapsulation is), except other classes instantiate them or extend them. – Himanshu Bhandari Feb 27 '16 at 10:18
  • Possible duplicate of http://stackoverflow.com/questions/1020749/what-are-public-private-and-protected-in-object-oriented-programming The **very first** answer to that question probably answers your question. This site has a search function; use it before posting a new question. – Raedwald Feb 27 '16 at 21:49
  • @ Raedwald : Thanks for the info but I am aware of it and the link you provided is not answering my question, at least I couldn't find what I wanted. – Himanshu Bhandari Feb 29 '16 at 05:40
  • I have updated my question, so please open it for answers. – Himanshu Bhandari May 03 '16 at 09:29

2 Answers2

3

Heres another example that Encapsulation is Hiding data

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Which basically means you should change the access modifiers of the properties to private and allow access to the properties through methods

Referencing from https://docs.oracle.com/javase/tutorial/java/concepts/object.html

Arlind Hajredinaj
  • 8,380
  • 3
  • 30
  • 45
1

I dissagree. Encapsulation refers to hidden data that is manipulated only by the methods on the same object, it what makes sense of encapsulation:

encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as as single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.

Take a look here, is correctly explained. http://www.tutorialspoint.com/java/java_encapsulation.htm

Oldskultxo
  • 945
  • 8
  • 19