Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S07L24 - Access modifiers in Java with inheritance

Course Articles
0 views

S07L24 - Access modifiers in Java with inheritance


February 13, 2025

Access Modifiers in Java with Inheritance

Table of Contents

  • Introduction
  • Access Modifiers Overview
    • Public
    • Private
    • Default
    • Protected
  • Access Modifiers and Inheritance
  • Comparison of Access Modifiers
  • Access Modifiers Syntax
  • Practical Java Program
  • Best Practices
  • Conclusion
  • SEO Keywords

Introduction

In Java, access modifiers are fundamental to controlling the visibility and accessibility of classes, methods, and variables. Whether you are building a small program or a large-scale system, proper usage of access modifiers ensures that your code is organized, secure, and easy to maintain.

In this article, you will learn:

  • The role of Java access modifiers.
  • Their use in inheritance.
  • Real-world coding examples.
  • Best practices for clean code.

Access Modifiers Overview

What are Access Modifiers?

In Java, access modifiers control which other classes or packages can access a specific class, method, or variable. These modifiers enforce encapsulation, a core principle of object-oriented programming.

Types of Access Modifiers

Access ModifierClassPackageSubclassWorld
public✔✔✔✔
protected✔✔✔✘
default✔✔✘✘
private✔✘✘✘

When and Where to Use:

SituationRecommended Access Modifier
Public APIs or Utility Classespublic
Sensitive information restricted to classprivate
Shared within the same packagedefault
Shared with subclassesprotected

Access Modifiers and Inheritance

When using inheritance, access modifiers significantly influence the visibility of superclass members inside subclasses.

Member TypeSame Package (Subclass)Different Package (Subclass)
public✔✔
protected✔✔
default✔✘
private✘✘

Syntax and Code Explanation

Syntax Example

// Access Modifiers Syntax
public class Base {
    private int privateVar;
    int defaultVar;
    protected int protectedVar;
    public int publicVar;
}

Practical Java Program (From Project Files)

File 1: Base.java

package org.studyeasy;

public class Base {
    private int privateVar = 10;
    int defaultVar = 20;
    protected int protectedVar = 30;
    public int publicVar = 40;

    public void show() {
        System.out.println("privateVar: " + privateVar);
        System.out.println("defaultVar: " + defaultVar);
        System.out.println("protectedVar: " + protectedVar);
        System.out.println("publicVar: " + publicVar);
    }
}

File 2: Sub.java

package org.studyeasy;

public class Sub extends Base {
    public void show() {
        // System.out.println("privateVar: " + privateVar); // Not accessible
        System.out.println("defaultVar: " + defaultVar);
        System.out.println("protectedVar: " + protectedVar);
        System.out.println("publicVar: " + publicVar);
    }
}

File 3: Main.java

package org.studyeasy;

public class Main {
    public static void main(String[] args) {
        Base base = new Base();
        base.show();

        Sub sub = new Sub();
        sub.show();
    }
}

Code Explanation (Step by Step)

  • The class Base defines variables with all four types of access modifiers.
  • The show() method in Base prints all variables, even the privateVar since it's in the same class.
  • The subclass Sub tries to access these variables:
    • privateVar: ❌ Not accessible.
    • defaultVar: ✔ Accessible if in the same package.
    • protectedVar: ✔ Accessible due to protected.
    • publicVar: ✔ Accessible universally.
  • The Main class creates instances of both Base and Sub and calls their show() methods.

Program Output:

privateVar: 10
defaultVar: 20
protectedVar: 30
publicVar: 40
defaultVar: 20
protectedVar: 30
publicVar: 40

Best Practices for Access Modifiers

  • Use private for variables unless you have a strong reason not to.
  • Use protected when subclasses need access.
  • Avoid public fields directly, prefer using getters/setters.
  • Keep default access for package-level utility classes.
  • Don't forget to update access modifiers when refactoring.

Conclusion

Access modifiers are the foundation of secure and organized Java applications. They help:

  • Maintain encapsulation.
  • Control access between classes and packages.
  • Build scalable and maintainable codebases.

By understanding how to use public, protected, default, and private correctly—especially in inheritance—you'll be able to write professional and clean Java code.

Tags:
access modifiers in Java
Java access modifier example
Java access modifiers
Java coding best practices
Java encapsulation
Java inheritance access
Java OOP access modifiers
Java visibility control
public protected private default Java
Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.