🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Classes and Objects: all 10 questions

Quiz: Java Classes and Objects · Question 9 of 10

What is the main benefit of private fields with public methods (encapsulation)?

What is the main benefit of private fields with public methods (encapsulation)?

  1. A.The program runs faster
  2. B.Fields use less memory
  3. C.The class can check every change (for example, no negative balance) and change its internals without breaking other code
  4. D.Other classes can change the fields directly
Answer & explanation

Correct answer: The class can check every change (for example, no negative balance) and change its internals without breaking other code

Hiding the data lets the class protect its own rules; outside code can use only the methods it offers.

Can you get the rest right without peeking?

Take the timed Quiz: Java Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →