PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Access Modifiers in Java

← All stacks

Theory

48/270

Access Modifiers in Java

Access modifiers control who can see a class member. private: only the same class. default (no word): same package. protected: package + subclasses. public: everywhere.

Start strict (private) and open only when needed. Making everything public “for speed” breaks encapsulation.

Recite narrow to wide — private → default → protected → public. That sentence alone clears most MCQs.

Let's take this on the board with one tiny Main class — no extra files. Access Modifiers in Java — secret is private; outside code uses a getter. Start from main, go line by line, and stop at each print. That output is the proof for Access Modifiers.

Exam tip

Narrow → wide: private, default, protected, public.

Example

// Same-file demo of access idea
class Box {
  private int secret = 7;
  int getSecret() { return secret; } // package/public API
}
public class Main {
  public static void main(String[] args) {
    System.out.println(new Box().getSecret());
  }
}

Access Modifiers in Java — secret is private; outside code uses a getter.

Short notes

  • DefAccess modifiers control who can see a member.
  • Ruleprivate = class. default = package. protected = package + child. public = all.
  • RememberStart private. Open only when needed.
  • TrapEverything public breaks encapsulation.

Questions

1

Who can see a private member?

2

What is default access?

3

Who can see protected?

4

Order from narrow to wide?

Previous← Packages in JavaNextEncapsulation in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.