Page 259 - ComputerScience_Class_11
P. 259

The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java
                   Options

                  The Area of a Circle: 98.53311999999998
                  The Area of a Rectangle: 10
                  The Area of a Square: 25

                 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 multiple methods with the same name, but with different parameter
                 types or different numbers of parameters. This is a form of polymorphism. In the case of method overloading, it
                 is a type of compile-time polymorphism (also known as static polymorphism), where the method to be invoked is
                 determined by the compiler based on the method signature and the arguments provided. The compiler finds the best
                 match for the method call at compile-time, which is called static binding.

                     9.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.


                 9.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++)
                            {



                                                                                           Methods and Constructors  257
   254   255   256   257   258   259   260   261   262   263   264