Page 208 - computer science (868) class 11
P. 208

In the above program, there are three methods:
              •  area(double r) and area(int s) have one parameter each but the type of parameters are different.
              •  area(int s) and area(int l, int b) have the same type of parameters but the number of parameters is different.
              Thus, method overloading is the process of defining functions/methods which have the same name but with different
              number or types of parameters. It follows the concept of Polymorphism. When the method is called, the compiler
              finds the function which is the best match, depending upon the arguments. This finding of the best-matched method
              at runtime is known as dynamic binding.

                   8.9 OBJECTS AND CLASSES
              An object is the most fundamental unit of object-oriented programming. It is a real-life entity and is an instance of a
              class. Objects have properties that define them, as well as functions and methods associated with them.
              A class on the other hand is a logical entity and consists of the characteristics and behaviour which are common to all
              the objects in that class.

              Classes, thus act as a blueprint for creating objects of the same type. They are non-primitive data types that are
              created by the user as and when required.

              For example, if we consider a class named Books, then the different objects in the class can be Story books, Comic
              books, Workbooks, and so on. Each of these different objects has the general characteristics of the books (as they get
              it from the class Books) and yet are different with their own attributes like genre type, author, year of publication, etc.

              8.9.1 Creation of an object of a class
              The syntax of creating an object of a class is:

                  [class_name] [object_name] = new [constructor with or without parameter];
              For example,

                  Factorial ob = new Factorial ();
              Let us define a class:
                  class factorial
                  {
                      int n, f;
                      factorial (int temp)
                      {
                          n=temp;
                          f=1;
                      }
                      private void calculate ()
                      {
                          int i;
                          for (i=1; i<=n;i++)
                          {
                              f=f*i;
                          }
                      }

                      void display ()
                      {
                          calculate();
                          System.out.println("Factorial:" +f);
                      }
                  }
              Let us discuss some of the important terms mentioned above.
              a. Access Specifier: To get accessed in different ways, we need an Access specifier. The three-access specifiers are:
                 Public, Protected and Private. They are also known as Visibility modes.


                206206  Touchpad Computer Science-XI
   203   204   205   206   207   208   209   210   211   212   213