Page 29 - CA_Blue( J )_Class10
P. 29

2.2 OBJECT
                 Occurrence of a class that contains characteristics and behaviour of the same class are called objects. In other words,
                 objects are the instance of a class. Thus, an object implements a class. The different components of an object are:
                 •  Characteristics or attributes           •  Behaviour or methods      •  Name of the object

                 2.2.1 Creating Objects of a Class
                 Instance of a class is also called an object and creating an object of a class is called instantiation. Syntax of creating
                 an object:

                    <class_name> <object_name> = new <constructor>;
                 For example:

                    book computer = new book();
                 The new is a keyword used to create an object of a class. The book() is a constructor. A constructor is a function or
                 method that has the same name as the class and is used for initialising an object created of the class “book”.
                 Taking the above example:

                                 public class ObjectExample
                                 {
                                         public static void main ()
                                         {
                                              Square sq= new Square (); // an object named sq of
                                               class Square has been created
                                             sq.accept(); // Calling accept() method of sq object s
                                             sq.area(); // Calling area() method of sq object
                                             sq.perimeter(); // Calling perimeter() method of
                                             sq object;
                                         }
                                     }
                                 }


                 Here, “sq” is the object created with the help of the “new” operator. Also “accept()”, “area()” and “perimeter()”
                 functions of the object sq are called.


                     2.3 PROPERTIES OF CLASS AND OBJECT
                 Class and object have some properties. Let us discuss them in detail.


                 2.3.1 Class is an Object Factory
                 Factories are places that produce products of the same kind. In the same way, a class acts as a factory as it is used to create
                 similar types of objects with different characteristics and common behaviours. Hence, a class is called an object factory.
                 The this keyword is used to refer to the current instance of the class within instance methods and constructors. It
                 allows you to access instance variables and methods of the current object.

                 For example:

                                 // Define a class called Bus
                                 class Bus
                                 {
                                     String color;      // Fields (attributes)
                                     String model;
                                     // Constructor


                                                                                                                       27
                                                                                 Elementary Concept of Objects and Classes  27
   24   25   26   27   28   29   30   31   32   33   34