Page 266 - CA_Blue( J )_Class10
P. 266

11.5.1 Access Specifiers
              The different components in a class can be accessed by different
              parts from within class or outside the class. The access specifiers        Definition
              restrict the scope of the uses of data members and member
              methods within the class or outside the class. This means the   Access specifiers are used to limit the accessibility
              visibility  of  the data members and  member  methods  can be   of a certain part of a class. These are also known
              controlled by these access specifiers. Java provides three access   as Visibility specifiers.
              specifiers which are private, public and protected.

              The private Specifier
              The data members or member methods that are declared with the private access specifier can be accessed only within
              the class. Examples of private data members and member methods are:
                     private int a, b;
                     private double c, d;
                     private void sum()
              The public Specifier
              The data members or member methods that are declared with the public access specifier can be accessed within the
              class and outside the class also. Examples of public data members and member methods are:

                     public int a,b;
                     double c,d;  // by default, a data member is public
                     public void sum()
                     void difference() // by default, a member method is public
              The protected Specifier
              The data members or member methods that are declared with the protected access specifier can be accessed within
              the class and inherited class also. Examples of protected data members and member methods are:

                     protected int a, b;
                     protected double c, d;
                     protected void calculate()
              The following table shows the visibility scope of all the access specifiers:
                    Access Specifiers           Same class               Inherited class            Other class
               private                   Can be accessed           Cannot be accessed        Cannot be accessed
               protected                 Can be accessed           Can be accessed           Cannot be accessed
               public                    Can be accessed           Can be accessed           Can be accessed

              11.5.2 Data Members
              There are two types of data members used in a class. They are instance variables and class variables.

              Instance Variables
              Instance variables are also known as non-static data members. These variables are declared within the class but
              outside the member methods. They are created whenever any object is generated. The instance variables associated
              with different objects contain different values. For example:
                  class IVariables
                  {
                      int a;
                      double b;
                      //member methods
                  }


                264264  Touchpad Computer Applications-X
   261   262   263   264   265   266   267   268   269   270   271