Page 230 - Computer science 868 Class 12
P. 230

Let us study in detail.

              8.3.1 Header
              The first line of any method definition is known as the method header or method prototype. It consists of the access
              specifier, return data type, method name and parameter list.

              Let us consider the following example.

                                           public  int  perfect  (int num)


                                                                         Parameter List
                                                                         Method Name
                                                                         Return Type

                                                                         Access Specifier

              8.3.2 Method Signature
              Only the name of the method along with the parameter list in the method prototype is known as the Method Signature.


                                                         perfect(int num)

              8.3.3 Access Specifier
              We know that the methods are accessed by other methods or classes. Therefore, there should be some criteria as well
              as the restrictions on the accessibility of the method. This is done and ensured by the Access Specifiers. The access
              specifiers define how the methods (or class or variables) of a class can be accessed. There are three types of access
              specifiers in Java which are discussed below.
              •   Public: It is used when we want all the other classes and methods to access the required method. In this case, the
                 method prototype is preceded by the keyword public.

                 Example:                          public int perfect(int num)

              •   Private: It allows only the methods of the same class to be accessed. Private access specifier has the most restrictions.
                 The word private is preceded by the prototype of the method. The prototype of the method is preceded by the
                 keyword private.

                 Example:                          private int perfect(int num)

              •   Protected: This is a special type that allows the methods of the same class and the child class to access the required
                 method. By using inheritance, the protected access specifier can access the methods outside the package.

                Example:                            protected int perfect(int num)


              8.3.4 Return Type and Return Statement
              A function can only be used when it is called from another function. So, after completing the assigned job, the control
              returns back to the portion from where it is called. While doing this, it may or may not return a value along with it. For
              these reasons, we require Return Type and Return Statement.
              •   Return Statement: This is required if the function is returning some value. The returned value may be of any data
                 type, even non-primitive data types.
                If a method does not return anything, then the keyword “void” is written.


                228228  Touchpad Computer Science-XII
   225   226   227   228   229   230   231   232   233   234   235