Page 46 - CA_Blue( J )_Class9
P. 46

Defining a Class in Java
                  Let us define a class circle:

                  class circle
                      {
                         //Data Members
                         double radius;       // used for storing the radius of the circle
                         //Member Methods
                         void input()         // Accepts the radius from the user
                         void area()          // Calculates the area of the circle
                         void diameter()  // Calculates the diameter of the circle
                      }
                  Here, "radius" is data member or attribute and "input ()", "area ()" and "diameter ()" are the member methods.



                       3.3 OBJECT IN JAVA
                  In everyday life, we encounter various objects, such as books, school uniforms, and buildings. These objects have
                  their own characteristics (attributes) and behaviours (methods), which are based on the structure or blueprint from
                  which they were created. For example, if "Fan" is a class, then individual fans produced by different companies are
                  objects of that class.

                  An object in Java is an instance of a class that contains the characteristics (attributes) and behaviours (methods)
                  defined by the class. Objects are the fundamental units of Object-Oriented Programming, and we can say that an
                  object is an implementation of a class. The key components of an object are:
                  •  Characteristics or Attributes: These are properties that define the state of an object. They help distinguish one
                     object from another by holding unique values for each instance.
                  •  Behaviour or Methods: These are actions or operations that an object can perform, defined by the methods
                     in the class.
                  •  Name of the Object: Each object can have a unique name, allowing it to be identified and referenced within a
                     program.

                  3.3.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();
                                                              Definition


                          Constructor is a function  or method that has same name as that of the class and used for
                          initializing an object created by the class.



                        Note: In Java, if you do not define a constructor for a class explicitly, the Java compiler provides a default
                        constructor implicitly.


                  Creating objects for class Circle:
                      public static void main ()



                    44    Touchpad Computer Applications-IX
   41   42   43   44   45   46   47   48   49   50   51