Courseiva
Object-Oriented ProgrammingeasyMultiple ChoiceObjective-mapped

Encapsulation: Using Getter Methods for Read-Only Access

In a banking application, a class 'Account' has a private field 'balance'. Which is the best way to allow subclasses to read but not directly modify 'balance'?

Quick Answer

The correct answer is to provide a public getBalance() method. This follows the encapsulation getter method best practice by exposing a read-only access point to the private field 'balance' while preventing subclasses from directly modifying it. In object-oriented programming, encapsulation protects internal state, and a getter is the standard Java pattern for controlled read access—subclasses can call getBalance() to retrieve the value but cannot assign to the private variable. On the Oracle Java Foundations 1Z0-811 exam, this concept tests your understanding of access modifiers and the principle of data hiding; a common trap is choosing a protected field, which would allow direct modification from subclasses. Remember the mnemonic: “Private field, public getter—read only, no setter.”

⚠ Common exam trap

Oracle often tests the misconception that protected access is sufficient for read-only access, but protected actually allows both reading and writing by subclasses, failing the 'not directly modify' requirement.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Provide a public getBalance() method

It follows encapsulation: a public getBalance() method provides read-only access to the private balance field. Subclasses cannot directly modify balance because it is private. Option D (protected) is incorrect because protected access allows subclasses to directly read and write the field, which violates the requirement that subclasses should not be able to directly modify balance.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Make balance public

    Why it's wrong here

    Public allows direct modification, violating encapsulation.

  • Provide a public getBalance() method

    Why this is correct

    Getter provides read-only access; keep field private.

  • Use a static variable

    Why it's wrong here

    Static is not related to access control; all accounts would share the same balance.

  • Make balance protected

    Why it's wrong here

    Protected allows subclasses to modify directly.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. In a banking application, a class 'Account' has private field 'balance' and public methods 'getBalance()' and 'setBalance(double)'. This is an example of which OOP principle?

easy
  • A.Abstraction
  • B.Inheritance
  • C.Polymorphism
  • D.Encapsulation

Why D: Encapsulation hides internal data (the 'balance' field) and provides controlled access via public methods 'getBalance()' and 'setBalance(double)'. Option A is wrong because abstraction focuses on hiding implementation complexity rather than data protection; the example demonstrates data hiding which is encapsulation. Option B is wrong because inheritance involves a parent-child class relationship, which is not shown. Option C is wrong because polymorphism requires method overriding or overloading, which is not present here.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.