Page 347 - CA_Blue( J )_Class10
P. 347

This means that the data members and member methods preceded by the access specifier public can be accessed
                 from anywhere, either within the class or outside the class.


                  Program 2    To print the greater of 4 and 5.


                   1  import java.util.*;
                   2  class public_access_sp
                   3  {

                   4      public int a,b;

                   5      public void accept(int a1,int b1)
                   6      {

                   7      a=a1; b=b1;
                   8      }

                   9      public void greater()
                  10      {
                  11         if(a>b)

                  12               System.out.println("Greater Number:" + a);

                  13         else
                  14               System.out.println("Greater Number:" + b);
                  15      }

                  16  }
                  17  class other_class

                  18      {
                  19      public static void main()

                  20      {
                  21      public_access_sp ob=new public_access_sp();

                  22      ob.accept(4,5);
                  23      ob.greater();

                  24      }
                  25  }

                 You will get the following output:

                 Greater Number: 5

                 14.2.3 The protected Access Specifier
                 The protected access specifier allows data members and member methods can be accesses within the package. It also
                 allows the data members and member methods can be accessed outside the package through inheritance. You will
                 learn about inheritance later in this chapter.




                                                                                                                       345
                                                                                           Encapsulation and Inheritance   345
   342   343   344   345   346   347   348   349   350   351   352