Page 226 - CA_Blue( J )_Class10
P. 226

Method Signature
              Method signature is a part of method header containing only the name of the method along with parameter list. For
              example,

                                                       sum (int i, int j)

              Access Specifier
              Since methods can be accessed by other classes, the same class, or other methods, we need to apply some restrictions
              on the accessibility of the methods. This is done by the access specifiers. An access specifier or access modifier specifies
              the boundary for a method in which it is accessible. There are three types of access specifiers in Java which are as
              follows:
              •  The public Access Specifier: The public access specifier allows us to give permissions to a method accessible
                  anywhere inside the class and outside the class. For example,

                                                public int sum (int i, int j)
              •  The private Access Specifier: The private access specifier restricts a method accessibility outside the class. Only
                  the methods of the same class can access a private method. For example,

                                               private int sum (int i, int j)

              •  The protected Access Specifier: The protected is a special type of access specifier which allows methods of the
                  same class and inherited class can access a protected method. For example,

                                              protected int sum (int i, int j)


              Return Type and Return Statement
              A method is used only when it is called by another method. So, after completing the assigned task, the method returns
              control to the portion from where it is called. While returning the control, a method may or may not return a value
              along with it. For this purpose, we require return type and return statement.
              •  The return Statement: The return statement is required if the function is returning some value. The returned value
                  may be of any data type, even non-primitive data types. Following are the characteristics of return statement:
                  ▪   It is used to return a value from the method to the calling program.
                  ▪   It is always the last line of a method.
                  ▪     By default, it always returns a single value of one data type only. If we want to return more than one value, we
                     have to create a non-primitive data type and return. Example,


                                              int[] sum ()
                                              {
                                                      int i;
                                                      int ar[] = {1,2,3,4,5};
                                                      for (i=0; i<5; i++)
                                                      {
                                                               ar[i] = ar[i]+2;
                                                      }
                                                      return ar;
                                              }











                224224  Touchpad Computer Applications-X
   221   222   223   224   225   226   227   228   229   230   231