Behavior of Access Modifiers in case of 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:

1. 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.

2. The default members of the parent class can be inherited from the derived class within the same package.

3. The protected members of a parent class can be inherited to a derived class, but the usage of protected members is limited within the package.

4. Public members can be inherited from all subclasses.

Java Inheritance Example Programs


Let’s take an example where we will understand private members of superclass are not accessible or visible in subclass but protected members are available in subclass.

Program code 1:

package inheritance; 
public 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"); 
  } 
} 
public 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


Look at the below screenshot. When we call m1() method using the subclass reference variable, m1 is not available to call. But m2() method is available. Similarly, private variable x is also not available to subclass.
Behavior of Access Specifiers in case of Java Inheritance


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

Program code 2:

package inheritance; 
public 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); 
   } 
} 
public 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.


Let’s take an example program in which we will understand the effect of access modifiers in the different packages.


Program code 3:

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. 
 } 
}

Program code 4:

package package1; 
public class AA 
{ 
  int x = 10; 
  protected int y = 20; 
  public int z = 30; 
} 
public class BB extends AA 
{ 
   public int z = 100; 
} 
public 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

In this tutorial, we have explained the behavior of access modifiers in the case of Java inheritance with various example programs. Hope that you will have understood the basic points and practiced all example programs. In the next, we will understand types of inheritance with examples.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love