Tuesday, August 9, 2016

Protected Access Modifier

In the previous post, you learned how to private and public access modifier work. Now we will discuss another access modifier in Java, which is protected. As previously mentioned, the derived class still can not access the private part of its parent class. In certain cases, sometimes we need data that can only be accessed by classes that have child relationship, without allowing the outside environment to access the data. In this situation, we can not use private access modifier for data, because private is only recognized by a particular class only. Not too public, because if we use this access level, the external environment is still able to access the data. The right solution for such problem is by using a access modifier protected. In this way, the data can be accessed by all classes that have child relationship, but the external environment were not given the right to access the data.

Java provides keyword protected to declare protected data. Here is an example of a program that can show how protected access modifier work:

class A {

   private int a; //only recognized by class A

   //only recognized by class A and its derivative class
      protected void setA (int value) {
      a = value;
   }

   //only recognized by class A and its derivative class
   protected int getA() {
      return a;
   }
}

class B extends A {

   private int b;

   //constructor class B
   B(int valueA, int valueB) {
      //a = valueA; //INCORRECT, because a is not recognized here
      setA(valueA); //using method setA()
      b = valueB;
   }

   public void showData() {
      //using method getA
      System.out.println("Value a : " + getA());
      System.out.println("Value b : " + b);
   }
}

class C {
   private int c;

   public void setC(int value) {
      //set A (10); //INCORRECT, setA() is not recognized here
      c = value;
   }

   public int getC() {
      return c;
   }

   public void showC() {
      //System.out.println("Value a : " + getA()); //INCORRECT
      System.out.println("Value c : " + c);
   }
}

Class DemoProtected1 {
   public static void main(String[] args) {

      B obj = new B(40, 50);

      obj.showData();

      obj.setA(100);

      System.out.println("Value a : " + obj.getA()):
   }
}

The above program will produce following output:

Value a: 40
Value b: 50
Value a: 100

In the program above, we define method setA() and getA() in class A with protected. Thus, the method can be accessed by derived class (in this case class B). However, when we access the method of class C, there will be error during the compilation. This is because the class C does not have a hierarchical relationship to class A. Here, class C is considered as an external environment which is not allowed to access the protected data from class A.

3 comments:

  1. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete