Page 349 - CA_Blue( J )_Class10
P. 349

33         System.out.println("Enter a number :");
                  34         n=sc.nextInt();

                  35      }
                  36  }

                 You will get the following output:

                 Enter a number : 5
                 The factorial of 5 is : 120


                       Note: In the preceding code, the extends keyword is used. This keyword allows us to inherit a class.




                 14.2.4 Visibility Rules for 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


                     14.3 SCOPE OF VARIABLES
                 Variables are name given to a memory location to store values. The use of these variables in the program code can be
                 restricted using the concept of scope. There are mainly four type of variables in Java which are local, class, instance
                 and argument.
                 14.3.1 Local Variables
                 Some variables have very limited scope. They can be accessed only within the method as they are declared within the
                 curly brackets of a method. These variables are known as local variables.
                 For example:


                  Program 4    To calculate the factorial of a number.


                   1  import java.util.*;
                   2  class factorial

                   3  {
                   4      int n;

                   5      void input()
                   6      {

                   7          Scanner sc= new Scanner(System.in);
                   8          System.out.println("Enter a number: ");

                   9          n=sc.nextInt();
                  10      }





                                                                                                                       347
                                                                                           Encapsulation and Inheritance   347
   344   345   346   347   348   349   350   351   352   353   354