ICSE Class 10 Computer Applications Question 4 of 16

Encapsulation and Inheritance — Question 4

Back to all questions
4
Question

Question 4

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

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.