5. Understanding Protected Access Modifier in Java
The protected access modifier in Java allows access within the current package and also from outside the package but only through child classes. Before delving into protected access, let's briefly review the concepts of child class and parent class.
A class serves as a template or blueprint for objects, defining their states and behaviors. A child class, or subclass, inherits from a parent class, also known as a superclass or base class.
- Class - a group of objects that have states and behavior
- Child/Sub Class - is a class that inherits from its parent class
- Parent/Super/Base Class - is the class from which its child inherits features
5.1 Example – Child Class and Parent Class
In this example, we create a Java file named X.java
. Inside this file, there are three classes: A
, B
, and X
. Class A
contains method m1
and is the parent of class B
. In class X
, we create objects from class A
and class B
and call m1
using the object created from class B
.
// Code X.java
class A {
public void m1() {
System.out.println("This is m1 from class A");
}
}
class B extends A {
public void m2() {
System.out.println("This is m1 from class B");
}
}
class X {
public static void main(String[] args) {
A a = new A();
a.m1();
B b = new B();
b.m1();
}
}
Upon compilation, the code works, and the diagram illustrates how the child class B
inherits the method from its parent class A
.
5.2 Example - Accessing Protected Method within the Same Package
In this example, we create a Java file named C.java
. Inside this file, there are two classes: P
and C
. Class P
contains a protected method m1
and is the parent of class C
. In class C
, we create objects of class P
and class C
and call m1
of class P
and class C
.
// Code C.java
package pack1;
class P {
protected void m1() {
System.out.println("This is protected method m1 from class P");
}
}
class C extends P {
public static void main(String[] args) {
// First method
P par = new P();
par.m1();
// Second method
C chi = new C();
chi.m1();
// Third method
P par_chi = new C();
par_chi.m1();
}
}
Upon compilation, the code works, and we understand the three possible ways of accessing the protected method within the same package.
5.3 Example - Accessing Protected Method from a Different Package
In this example, we create two Java files named P.java
and C.java
. Inside P.java
, there is a package statement pack1
, and class P
contains a protected method m1
.
// Code P.java
package pack1;
public class P {
protected void m1() {
System.out.println("This is protected method m1 from class P");
}
}
Inside C.java
, there is a package statement pack2
, and class C
extends class P
. In class C
, we create objects from class P
and class C
and call m1
using objects created from class P
and class C
.
// Code C.java
package pack2;
import pack1.P;
class C extends P {
public static void main(String[] args) {
// First method
P par = new P();
par.m1();
// Second method
C chi = new C();
chi.m1();
// Third method
P par_chi = new C();
par_chi.m1();
}
}
Upon compilation, an error occurs, highlighting that accessing the protected member from outside the package requires using the child class reference only. The third method demonstrates the correct way to access the protected member from outside the package.