Behavior of Access Modifiers in Java Inheritance

Superclass members can be inherited to subclass provided they are eligible by access modifiers. The behavior of access modifiers in the case of inheritance in Java is as follows:

  • The private members of the superclass cannot be inherited to the subclass because the private members of superclass are not available to the subclass directly. They are only available in their own class.
  • The default members of the parent class can be inherited by the derived class only if the derived class is in the same package.
  • The protected members of a parent class can be inherited within the same package and also in derived classes outside the package, but only through inheritance.
  • Public members of a superclass can be inherited and accessed by any subclass, anywhere, regardless of the package.

Examples of Behavior of Access Modifiers in Inheritance


Let’s take some important examples based on the behavior of access modifiers in Java inheritance.

Example 1: Accessing Private and Protected Members in Inheritance

class Baseclass 
{ 
  private int x = 30; 
  protected int y = 50; 
  private void m1() 
  { 
     System.out.println("Base class m1 method"); 
  } 
  protected void m2() 
  { 
     System.out.println("Base class m2 method"); 
  } 
} 
class Derivedclass extends Baseclass 
{ 

} 
public class MainClass { 
public static void main(String[] args) 
{ 
// Creating an object of subclass.
   Derivedclass d = new Derivedclass(); 

// We cannot access private members due to not being available in the subclass.
// System.out.println("x = " +d.x);
// d.m1();
 
// Accessing the protected members from the subclass.
   d.m2(); 
   System.out.println("y = " +d.y); 
 } 
}

Output:

Base class m2 method 
y = 50

In this example, we have defined a class Baseclass containing private and protected members (variables and methods). Then, we have defined a subclass Derivedclass that extends Baseclass. When we call m1() method using the subclass reference variable, m1 is not available to call. This is because private members of the superclass are not accessible or visible in the subclass.

On the other hand, m2() method is available because protected members of the superclass are available in the subclass within the same package. Similarly, private variable x is also not available to subclass.

Behavior of Access modifiers in Java Inheritance

Example 2: Accessing Default and Public Members in Inheritance

Let us take an example to understand how default and public members of superclass are accessible in the subclass.

class Identity 
{ 
   String name = "Deep"; // default member.
   public int age = 28; // public member.

// Default method.
   void m1()
   { 
     System.out.println("Name: " +name); 
   } 
// Public method.
   public void m2()
   { 
     System.out.println("Age: " +age); 
   } 
} 
class Person extends Identity 
{ 

} 
public class Mytest { 
public static void main(String[] args) 
{ 
    Person p = new Person(); 
    p.m1(); 
    p.m2(); 
    System.out.println("Name: " +p.name); 
    System.out.println("Age: " +p.age); 
 } 
}

Output:

Name: Deep 
Age: 28 
Name: Deep 
Age: 28

As you can see in the above program, default and public members can be easily accessible in the subclass within the same package.

Example 3: Effect of Access Modifiers in Different Packages

package package1; 
public class AA 
{ 
   private int a = 10; 
   int b = 20; 
   protected int c = 30; 
   public int d = 40; 
} 
package package2; 
import inheritance.AA; 
public class BB extends AA 
{ 

} 
public class AABB { 
public static void main(String[] args) 
{ 
    BB bb = new BB(); 

 // This statemet will generate compile-time error because we cannot access private members in the subclass.
    System.out.println(bb.a);  
    
 // This statement will generate a compile time error 
 // because we can access default members of the superclass in the subclass within the same package only.  
    System.out.println(bb.b);
    
 // This statement will produce a compile time error 
 // because we can access protected members of the superclass in the subclass within the same package only. 
    System.out.println(bb.c); 
    
    System.out.println(bb.d); // public members can be accessible anywhere. 
 } 
}

Example 4: Effect of Access Modifiers in Same Package

package package1; 
class AA 
{ 
  int x = 10; 
  protected int y = 20; 
  public int z = 30; 
} 
class BB extends AA 
{ 
   public int z = 100; 
} 
class CC extends BB 
{ 

} 
public class AABBCC { 
public static void main(String[] args) 
{ 
     BB bb = new BB(); 
     System.out.println(bb.x); // accessible because it is default.
     System.out.println(bb.y); // accessible in the same package because it is protected.
     System.out.println(bb.z); // public members are accessible anywhere in the program.

     CC cc = new CC(); 
     System.out.println(cc.x); 
     System.out.println(cc.y); 
     System.out.println(cc.z); 
  } 
}

Output:

10 
20 
100 
10 
20 
100

Conclusion

In inheritance, access modifiers play a crucial role in controlling the visibility and accessibility of superclass members in subclasses. Private members of a superclass are not accessible in the subclass, ensuring strong encapsulation and data hiding. Default (package-private) members are accessible only when the subclass belongs to the same package.

Protected members provide a balance between accessibility and security because they are accessible within the same package and also within subclasses in different packages through inheritance. Any subclass can inherit and access public members regardless of package.

We hope that you will have understood the behavior of access modifiers in the case of Java inheritance with the help of various examples. Stay tuned for the next tutorial.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.