Thursday, April 3, 2014

JAVA CONCEPTS ,Access Modifier - Part 5 (protected)

In the last post of modifier series, I have explained about the package-level access modifier .In this post I will explain about protected access modifier.

protected access modifier

protected access modifier means default(package-level) + something more. So we need to focus on something more part here.

protected means, protected = inheritance , but we can not consider it as public. there is some subtle difference between accessing public and protected members, that i will explain in this post.

To understand accessing of protected members we need to understand inheritance. I will explain inheritance in detail in OOPs concept, till now you can understand inheritance like if any class extends any other class then there will be a superclass and a subclass, in that situation subclass will inherit all property of superclass and subclass will pretend that property being inherited are member of subclass.
  • when we use protected access modifier with any class member then in that case  we can avail all the accessibility provided by the package level access modifier + accessibility to the subclass in different package also.
  • As the subclass in different package can access the protected members but there is some limitation.In this case if we create an instance of superclass in subclass that is present in different package ,then we can't access the protected members of superclass using the reference of superclass.
  • Means we can not use the dot operator with superclass reference to access protected member from subclass.
  • We can only access the protected members by considering, that member as inherited property and that will pretend that, that member is member of subclass only.
Example :
here we will write a class in a package and then we will write another class in different package and that class will inherit the class from first package using extend keyword.then we will try to access the protected member of superclass from subclass of different package, we will consider different scenarios.

let us write first class here in a package:

package protectedAccess;
// a class with protected access modifier attached with class members
public class Parent {
 protected int parentVariableB;
 
 protected void parentMethodA() {
  System.out.println("do something");
 }

}
Now we will write a class extending this class in different package.
package protectedAccess2; // different package

import protectedAccess.*;
// class extending Parent class.
1. public class Subclass extends Parent {

2. public void testing() { // method for testing purpose
3.  System.out.println("value of variable " + parentVariableB); // ok
4.  Parent pc = new Parent();
5.  int d = pc.parentVariableB;// compiler unhappy
6.  pc.parentMethodA();//  compiler unhappy

7.  Subclass sc = new Subclass();
8.  System.out.println(sc.parentVariableB); // compiler happy
9.  sc.parentMethodA(); // compiler happy

 }

 public static void main(String[] args) {
  Subclass sc = new Subclass();
  sc.testing();
 }

}

In the above written code :
Line no 1: At line no 1 , we created a new class extending another class of different package, means Parent is superclass and Subclass is subclass here.
Line no 2 : A method to test all concepts that had mentioned above and later we can call this method from main method to test.
Line no 3: We can access protected variable parentVariableB because due to inheritance this variable will be inheriting in this Subclass.
Line no 4: We create the instance of Parent class.
Line no 5: With the reference of Parent class and using dot (.) operator we are trying to access protected member, but here compiler will complain as follow
The field Parent.parentVariableB is not visible
    The method parentMethodA() from the type Parent is not visible

here it counts the difference between protected and public, if those class member would have been public then from Subclass also we could have access using the reference of Superclass, but here due to protected access modifier we can not access protected members using the reference of parent class.


Line no 6: here also compiler complain and provides same reason.
Line no 7: here we create an object of current class of this package(class Subclass)
Line no 8: here we can access the the protected member using the reference of Subclass because due to inheritance those members are inherited to subclass also and Subclass pretend that those members are their members defined like in Subclass only.
Line no 9: same reason as line no 8. 

Now there can be situation where any other class could extend Subclass in that package , then in that case how the inherited protected member will behave?
Example :
package protectedAccess2; // same package as Subclass
//class extending Subclass
1.public class AnotherSubclass extends Subclass {
2. public static void main(String[] args) {
3.  Subclass sc = new Subclass();
4.  System.out.println(sc.parentVariableB); // compiler unhappy
5.  sc.parentMethodA(); // compiler unhappy
  
6.  AnotherSubclass asc= new AnotherSubclass();
7.  System.out.println(asc.parentVariableB); //compiler happy
8.  asc.parentMethodA();//compiler happy
 }

}
In above code At :
Line no 3: creating the object of Subclass 
Line no 4: with the reference of superclass of AnotherSubclass trying to access inherited protected members again, so here again compiler complains like line no 5 of previous example.
Line no 7: compiler is happy and not restricting us, as those class member is again inherited to AnotherSubclass from Subclass.

summary: 
In case of protected members we can access protected members as like default level access modifier + inheritance for the class outside the package.

1 comment:

  1. This is really very helpful to understand the real meaning of access modifiers. You explained the context in simple and effective manner.

    ReplyDelete