Wednesday, July 13, 2016

Access Modifier: private, protected, and public

As we've mentioned earlier, in doing encapsulation, in fact we combine the data and code (method) into one. In these circumstances, we can determine access level of data and method, called access modifier, using keyword private, public or protected. In object oriented programming, data is normally designed to be private or only accessible to classes that have it all. Accessing data from the outside can only be done through method. This means, the method acts as the interface. Therefore, methods are generally public. Nevertheless, if we want to define methods that can only be accessed by the class itself, then we must declare it with the keyword private. One more thing, if we want the data and method that we defined in the class can be accessed by derived classes, the data and the method must be declared with the keyword protected.

In general, data and method encapsulation in a class can be written as follows:

class ClassName {
   access-modifier data1;
   access-modifier data2;
   ...
   access-modifier dataN;

   access-modifier method1 (list-of-parameters) {
      //code for method1
   }

   access-modifier method2 (list-of-parameters) {
      //code for method2
   }
   ...
   access-modifier  methodN (list-of-parameters) {
      //code for method
   }
}

In this case, access-modifier is one of keyword private, public, or protected. If we do not include one of these keywords when declaring the data or method, then by default Java will regard it as public. Consider the following example:

class AccessModifierExample {

   //by default, a is public
   int a;

   //b is declared as public
   public int b;

   //c is declared as private
   private char c;
   ...

   //by default, method1() is public
   int method1 () {...)

   //method2 is declared as protected
   protected void method2 () {...}

   //method3 is declared as private
   method3 private char () {...}
}

Here is an example of a program that will show the effect of public and private access modifier in a class. protected access modifier will be discussed in next post.
class AccessModifier {

   int a;
   public int b;
   private int c;

   public void setC(int value) {
      c = value;
   }

   public int getC() {
      return c;
   }
}

class PublicAndPrivateDemo {

   public static void main(String[] args) {

      AccessModifier obj = new AccessModifier();

      obj.a = 10; //CORRECT, because by default a is public
      obj.b = 20; //CORRECT, because b is public
      //obj.c = 30; //WRONG, because c is private
      obj.setC(30); //CORRECT, because method setC() is public

      System.out.println ("Value obj.a : " + obj.a);
      System.out.println ("Value obj.b : " + obj.b); 
      System.out.println ("Value obj.c : " + obj.getC);
   }
}


As you can see above, we declare data a and b contained in AccessModifier class as public, so that data can be accessed from the outside environment (in this case, the call is done in PublicAndPrivateDemo class). However, we declare data c as private so it is not accessible from the outside environment. Therefore, to access data c we need to define a method that serves as the interface, namely: method setC() and getC() which are both public. Method setC() is used to fill in the value of data c, while the method getC() is used to get the value of data c. Thus, although c is private, but indirectly also the outside environment can still access the data. Here is the result for above program:

Value obj.a : 10
Value obj.b : 20
Value obj.c : 30

Because it is public, then we can immediately call a and b value that contained in AccessModifier class in the following manner:

System.out.println ("Value obj.a : " + obj.a);
System.out.println ("Value obj.b : " + obj.b);

However, for data c, we can only call value through the method getC() using the following way:

System.out.println ("Value obj.c : " + obj.getC);

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