Computer Applications

Why is it a good idea to make all instance variables private?

Encapsulation & Inheritance in Java

2 Likes

Answer

It is a good idea to make all instance variables private because:

  1. It hides the implementation details of the class from other objects, which makes it easier to change the implementation without affecting the rest of the system.
  2. It provides encapsulation as the class defines a clear and well-defined interface for interacting with its state.
  3. It protects the state of the object by preventing external objects from modifying the state in an unintended way.

Answered By

1 Like


Related Questions