- packageA - Base * protectedMethod * packagePrivateMethod - Derived * protectedMethod * packagePrivateMethod - packageB - Derived * protectedMethod * packagePrivateMethodThe Base class is defined in packageA. It has two methods: (1) protedMethod and (2) packagePrivateMethod.
package methodOverride.packageA;
public class Base {
protected void protectedMethod() {
System.out.println("Base.protectedMethod()");
}
void packagePrivateMethod(){
System.out.println("Base.packagePrivateMethod()");
}
}
We also define a derived class in packageA. Both methods can be overridden. Here comes the confusing part. As mentioned previously, the derived class should not have the access to the package-private method of its base class but in this case because it is in the same package(packageA) it can access and override the Base.packagePrivateMethod.
package methodOverride.packageA;
public class Derived extends Base{
@Override
protected void protectedMethod() {
System.out.println("Derived.protectedMethod()");
}
@Override
void packagePrivateMethod(){
System.out.println("Derived.packagePrivateMethod()");
}
public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Derived();
Derived d1 = new Derived();
System.out.println("\nBase reference and Base instance");
b1.protectedMethod();
b1.packagePrivateMethod();
System.out.println("\nBase reference and Derived instance");
b2.protectedMethod();
b2.packagePrivateMethod();
System.out.println("\nDerived reference and Derived instance");
d1.protectedMethod();
d1.packagePrivateMethod();
}
}
We now define a derived class in a different package called packageB. As we can see the code blow, we can still override the protectedMethod but this can we can no longer override the packagePrivateMethod in packageA.Base. The reason is that derived class cannot access the package-private method of its base class and we are not in the same package where the base class is defined.
package methodOverride.packageB;
import methodOverride.packageA.Base;
public class Derived extends Base{
@Override
protected void protectedMethod() {
System.out.println("Derived.protectedMethod()");
}
void packagePrivateMethod() {
System.out.println("Derived.packageAccessMethod()");
}
}
If used in the same package, the package-private methods behave almost exactly the same compare to protected methods. Both of them are virtual methods; both of them are accessible everywhere in the same package(in our example is packageA).
The code below shows the use of the two methods:
package methodOverride.packageA;
public class Derived extends Base{
@Override
protected void protectedMethod() {
System.out.println("Derived.protectedMethod()");
}
@Override
void packagePrivateMethod(){
System.out.println("Derived.packagePrivateMethod()");
}
public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Derived();
Derived d1 = new Derived();
System.out.println("\nBase reference and Base instance");
b1.protectedMethod();
b1.packagePrivateMethod();
System.out.println("\nBase reference and Derived instance");
b2.protectedMethod();
b2.packagePrivateMethod();
System.out.println("\nDerived reference and Derived instance");
d1.protectedMethod();
d1.packagePrivateMethod();
}
}
Here is the output of the above program.
Base reference and Base instance Base.protectedMethod() Base.packagePrivateMethod() Base reference and Derived instance Derived.protectedMethod() Derived.packagePrivateMethod() Derived reference and Derived instance Derived.protectedMethod() Derived.packagePrivateMethod()Because package-private methods are not visible to other packages, we cannot use them in other places.
Conclusion
If used in the same package, package-private methods behave almost the same as protected methods. Because they are not visiable to other packages, it can only be used in other packages. Therefore the difference between the two is that protected methods are part of the API of the class(in the sense that they are exposed to the users of the class and they become parts of the developers' responsibility) while package-private methods are note. Or in other words, we can think package-private methods as proteced methods without being in the API. Essentially, if we want to use the polymorphism feature of Java but do not want to expose the details of the implementation, we use package-priavte methods.----- END -----
©2019 - 2022 all rights reserved