Superclass members can be inherited to subclass provided they are eligible by access specifiers. The behavior of access specifiers in 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 its own class.

2. The default members of the parent class can be inherited to 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 to all subclasses.

Let's create a program to understand private members of superclass are not accessible in subclass but protected members are available in subclass.
Program source 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) { Derivedclass d = new Derivedclass(); // Private members cannot be accessed due to not available in 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 subclass reference variable, m1 is not available to call. But m2() method is available. Similarly, private variable x is also not available to subclas.


Access specifiers in inheritance in java

Let us take another example program to understand default and public members of superclass that are accessible in the subclass.
Program source code 2:
    package inheritance; public class Identity { String name = "Deep"; public int age = 28; void m1(){ System.out.println("Name: " +name); } 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 one more example program to see the effect of access specifiers in the different packages.
Program source 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(); System.out.println(bb.a); // Compile time error because private members cannot be accessed in the subclass. System.out.println(bb.b); // Compile time error because default members of superclass can be accessed in the subclass within the same package only. System.out.println(bb.c); // Compile time error because protected members of superclass can be accessed in the subclass within the same package only. System.out.println(bb.d); // public members can be accessible anywhere. } }

Program source 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); System.out.println(bb.y); System.out.println(bb.z); 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
Final words
Hope that this tutorial has covered almost all the important points related to the behavior of access specifiers in case of java inheritance with example programs. I hope that you will have understood this topic and enjoyed them.

Please, share it on social networking sites for your friends if this topic is useful.
Thanks for reading!
Next ➤ Inheritance Example Programs in Java ⏪ Prev Next ⏩