Which of the following statements about private data members in Java is CORRECT?
hard📝 Conceptual Q10 of 15
Java - Encapsulation
Which of the following statements about private data members in Java is CORRECT?
APrivate data members are accessible by any class in the same package.
BPrivate data members cannot be accessed directly outside their class, even by subclasses.
CPrivate data members can be accessed directly by subclasses.
DPrivate data members are accessible globally if declared static.
Step-by-Step Solution
Solution:
Step 1: Recall private access rules
Private members are accessible only within the class they are declared.
Step 2: Analyze options
Private data members cannot be accessed directly outside their class, even by subclasses. correctly states that subclasses cannot access private members directly.
Step 3: Clarify misconceptions
Package access and static do not override private restrictions.
Final Answer:
Private data members cannot be accessed directly outside their class, even by subclasses. -> Option B
Quick Check:
Private means class-only access, no subclass access [OK]
Quick Trick:Private means accessible only inside declaring class [OK]
Common Mistakes:
Assuming subclasses can access private members
Confusing private with protected or package-private
Master "Encapsulation" in Java
9 interactive learning modes - each teaches the same concept differently