Question:

Which access modifier in Java is used to make a data member or a method member of a class visible only within the class?

Show Hint

Use private access modifier in Java to restrict class member visibility to within the same class.
Updated On: Jul 14, 2025
  • public
  • protected
  • default
  • private
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is D

Solution and Explanation

Java provides several access modifiers to control the visibility of class members (variables and methods).
- The public modifier makes the member accessible from any other class.
- The protected modifier allows access within the same package and also in subclasses.
- The default (no modifier) gives access within the same package only.
- The private modifier restricts access to within the class itself.
When we want to encapsulate data and restrict it from being accessed or modified outside the class, we use the private modifier.
This is an essential principle of object-oriented programming known as data hiding.
Thus, private is the correct modifier for keeping members strictly within the class.
Was this answer helpful?
0
0