Fatskills
Practice. Master. Repeat.
Study Guide: Java OOP-Encapsulation Access Modifiers private default protected public
Source: https://www.fatskills.com/surgery/chapter/java-oop-encapsulation-access-modifiers-private-default-protected-public

Java OOP-Encapsulation Access Modifiers private default protected public

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~4 min read

What This Is and Why It Matters

Access modifiers control the visibility and accessibility of classes, methods, and variables in Java. Understanding them is crucial for writing secure, maintainable, and efficient code. Incorrect usage can lead to security vulnerabilities, code that is hard to maintain, and bugs that are difficult to trace. For example, exposing sensitive data by using the wrong access modifier can compromise application security.

Core Knowledge (What You Must Internalize)

  • Access Modifiers: Control the visibility of classes, methods, and variables.
  • private: Accessible only within the same class. (Encapsulation and security)
  • default (no modifier): Accessible within the same package. (Package-level encapsulation)
  • protected: Accessible within the same package and by subclasses. (Inheritance and package-level access)
  • public: Accessible from any other class. (Wide accessibility)
  • Encapsulation: Hides the internal state and requires all interaction to be performed through an object's methods. (Maintains integrity and security)
  • Inheritance: Allows a class to inherit fields and methods from another class. (Code reuse and polymorphism)
  • Package: A namespace that organizes a set of related classes and interfaces. (Organization and scope control)

Step‑by‑Step Deep Dive

  1. Understand the Basics of Access Modifiers
  2. Action: Identify the four access modifiers.
  3. Principle: Each modifier controls the scope of access.
  4. Example: A private field is only accessible within its own class.
  5. ⚠️ Pitfall: Misunderstanding the scope can lead to unintended access.

  6. Apply private Modifier

  7. Action: Use private for fields and methods that should not be accessed outside the class.
  8. Principle: Encapsulation.
  9. Example: private int age;
  10. ⚠️ Pitfall: Overusing private can make testing difficult.

  11. Apply Default (Package-Private) Modifier

  12. Action: Use the default modifier for package-level access.
  13. Principle: Package-level encapsulation.
  14. Example: int salary;
  15. ⚠️ Pitfall: Forgetting that no modifier means package-private.

  16. Apply protected Modifier

  17. Action: Use protected for fields and methods accessible within the same package and by subclasses.
  18. Principle: Inheritance and package-level access.
  19. Example: protected void display() {...}
  20. ⚠️ Pitfall: Assuming protected is the same as public.

  21. Apply public Modifier

  22. Action: Use public for fields and methods accessible from any other class.
  23. Principle: Wide accessibility.
  24. Example: public String name;
  25. ⚠️ Pitfall: Overusing public can expose sensitive data.

  26. Combine Access Modifiers with Inheritance

  27. Action: Understand how access modifiers affect inherited members.
  28. Principle: Inheritance and polymorphism.
  29. Example: A protected method in a superclass is accessible in a subclass.
  30. ⚠️ Pitfall: Assuming inherited members retain the same access level.

How Experts Think About This Topic

Experts think about access modifiers in terms of encapsulation and security. They use the least permissive modifier necessary to minimize the risk of unintended access and modification. This approach helps maintain the integrity and security of the codebase.

Common Mistakes (Even Smart People Make)

  1. The mistake: Using public for all fields and methods.
  2. Why it's wrong: Exposes internal state, making the code vulnerable.
  3. How to avoid: Use private for fields and provide public getter/setter methods.
  4. Exam trap: Questions that ask for the most secure way to define a class.

  5. The mistake: Confusing protected with public.

  6. Why it's wrong: protected is more restrictive than public.
  7. How to avoid: Remember that protected is for package and subclass access.
  8. Exam trap: Scenarios involving inheritance and package access.

  9. The mistake: Forgetting the default modifier.

  10. Why it's wrong: No modifier means package-private access.
  11. How to avoid: Always specify an access modifier if unsure.
  12. Exam trap: Questions about default access levels.

  13. The mistake: Overusing private for methods.

  14. Why it's wrong: Limits testability and extensibility.
  15. How to avoid: Use private judiciously, especially for methods.
  16. Exam trap: Scenarios requiring method overriding.

Practice with Real Scenarios

Scenario: You are designing a BankAccount class with fields for accountNumber and balance.
Question: What access modifiers should you use for these fields? Solution: Use private for both fields to encapsulate the data.
Answer: private String accountNumber; private double balance; Why it works: Encapsulation protects the internal state of the object.

Scenario: You have a Employee class with a salary field that should be accessible only within the same package.
Question: What access modifier should you use? Solution: Use the default modifier (no modifier).
Answer: int salary; Why it works: Default access allows package-level encapsulation.

Scenario: You are creating a Vehicle class with a startEngine method that should be accessible by subclasses.
Question: What access modifier should you use? Solution: Use protected.
Answer: protected void startEngine() {...} Why it works: protected allows access within the package and by subclasses.

Quick Reference Card

  • Use the least permissive access modifier necessary.
  • private: Class-level access.
  • default: Package-level access.
  • protected: Package and subclass access.
  • public: Wide accessibility.
  • ⚠️ Pitfall: Overusing public exposes sensitive data.
  • Mnemonic: Private Default Protected Public (PDPP).

If You're Stuck (Exam or Real Life)

  • Check the scope of each access modifier.
  • Reason from the principles of encapsulation and security.
  • Use estimation to determine the least permissive modifier.
  • Refer to the Java documentation for detailed explanations.

Related Topics

  • Inheritance: Understand how access modifiers affect inherited members.
  • Encapsulation: Learn how to hide the internal state of an object.