Title: Access Modifiers in Java: public, private, protected
Open Graph Title: Access Modifiers in Java: public, private, protected
Description: Java access modifiers explained: public, protected, default and private, with a comparison table, examples and common interview questions.
Open Graph Description: Java access modifiers explained: public, protected, default and private, with a comparison table, examples and common interview questions.
Opengraph URL: https://thecodeforge.io/java/java-access-modifiers/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/java-access-modifiers/"
},
"headline": "Java Access Modifiers—Public Field Allows Negative Balances",
"description": "Java access modifiers explained: public, protected, default and private, with a comparison table, examples and common interview questions.",
"keywords": "Java Access Modifiers—Public Field Allows Negative Balances, Java, Beginner, Java Basics, A public double balance field allowed setting -100 on accounts. Use the compiler error debug guide to identify private access violations in Java.",
"proficiencyLevel": "Beginner",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-06T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/java/java-access-modifiers/",
"inLanguage": "en",
"educationalLevel": "Beginner",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java Access Modifiers—Public Field Allows Negative Balances", "item": "https://thecodeforge.io/java/java-access-modifiers/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the default access modifier in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The default access level in Java is called package-private. You get it by writing NO modifier at all before the field, method, or class. It means only classes within the same package can access that member. It's NOT a keyword — writing 'default int count = 0;' is actually a syntax error."
}
},
{
"@type": "Question",
"name": "Can a subclass access the private members of its parent class in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. Private members are strictly limited to the class they're declared in — not even subclasses can access them directly. If a parent class has a private field that subclasses need to work with, the parent should provide a protected getter method or use protected access on the field itself."
}
},
{
"@type": "Question",
"name": "What is the difference between private and protected in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Private restricts access to the single class where the member is declared — no other class can touch it, including subclasses. Protected is less restrictive: it allows access from the same class, any class in the same package, AND any subclass regardless of package. Use private when you want to hide implementation details completely, and protected when you're designing a class that's meant to be extended."
}
},
{
"@type": "Question",
"name": "Can I use 'default' as a keyword for access modifier?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. 'default' is a reserved keyword used only in switch expressions and default methods in interfaces. To make a member package-private, simply omit any access modifier. Writing 'default int x;' will result in a compiler error."
}
},
{
"@type": "Question",
"name": "What happens if I declare a class as private in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You cannot declare a top-level class as private — it's a compile error. Inner classes (nested classes) can be private, meaning they are only accessible within the enclosing class. This is useful for implementation details like tree nodes."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large