Which access specifier is usually used for data members of a class?

Options

  • A. Private
  • B. Public
  • C. Default
  • D. Protected

Correct Answer (Detailed Explanation is Below)

A. Private

Detailed Explanation

In Object-Oriented Programming, data members (variables inside a class) are usually declared as private to ensure encapsulation.

Private → Data can only be accessed within the class.
Public → Data can be accessed from anywhere (not recommended for data members).
Protected → Accessible within the class and derived classes.
Default → Depends on the language (not typically used for secure data hiding).

For example, in C++ and Java, data members are usually declared private and accessed using public getter and setter methods.

This supports the OOP principle of data hiding.

OOps! You are currently offline.