简体   繁体   中英

Java Encapsulation without getter setter methods

Can we achieve encapsulation in java without having any getter or setter methods?

For example, the code below

class A {

  private int id;
  private String name;

}

Can we say that above code satisfies encapsulation?

Sure, you can say that this class satisfies encapsulation. It does not expose internal state or implementation. If you add method that will use these fields it is encapsulation in action. The main idea of encapsulation is to hide internal implementation from the user of the class.

Partially, Actually there is literally no use of the code that you presented but making all the variables private alone will not make that an encapsulation.

Encapsulation means "binding the data and data members together" means joining data and methods related to that data in a class to form an encapsulated object.

And also to hide the direct usage of data members!.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM