AP CSA Unit 3 Day 27: Encapsulation Violation

Unit 3, Classes & Objects • Cycle 2
Day 27 Advanced Practice • Harder Difficulty
Focus: Encapsulation Violation Hard Encapsulation Violation

Advanced Practice Question

Format: Encapsulation Violation

Which design violates encapsulation?
// Option I
public class A {
    public int val;
}

// Option II
public class B {
    private int val;
    public void setVal(int v) {
        val = v;
    }
}

// Option III
public class C {
    private int val;
    public void changeVal(int delta) {
        val += delta;
    }
}
Difficulty: Hard  |  Topic: Encapsulation Violation  |  Cycle: 2 (Advanced)
Why This Answer?

I violates encapsulation by making val public (no control over access). II and III use private with controlled public methods (proper encapsulation).

Common Mistake
Watch Out!

Thinking any public method violates encapsulation.

AP Exam Strategy

Encapsulation = private data + public controlled access. Public variables violate it.

Master This Topic

This Cycle 2 HARD question tests encapsulation violation. Review Unit 3 concepts to build mastery of classes & objects.

  • Understanding encapsulation violation
  • Tracing code execution accurately
  • Avoiding common pitfalls
View Unit 3 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.