Page 45 - CA_Blue( J )_Class9
P. 45

3.2 CLASS IN JAVA
                 Object-Oriented Programming allows us to develop programs by using the concept of classes and objects. In Java,
                 the basic structure of a program is organised within a class, meaning the entire program is encapsulated (enclosed)
                 in a class.

                 Classes in Java are non-primitive data types that act as a blueprint for creating objects of the same type.
                 Class comprises characteristics and behaviour. It is a template where many objects share the same features
                 but having separate specifications for each feature. The syntax to declare a class is as follows:

                 accessSpecifier class ClassName {
                     // Data members (attributes or instance variables)
                     // Methods (functions or behaviors)
                 }
                 The description of these components are as follows:
                 •  Access  Specifier:  Access  specifiers  define  the  visibility  or  accessibility  of  classes,  its  methods,  and  data
                    members.  Common  access  specifiers  include  public  (accessible  from  anywhere),  private  (accessible  only
                    within the class), and protected (accessible within the package and subclasses).
                 •  "class" keyword: The class keyword is used to define a class in Java.
                 •  Class name: The class name is the identifier and is used to refer to a class.
                 •  Data members: Data members are variables defined within a class to represent the properties or state of an object.
                 •  Methods: Methods are functions defined within a class that describe the behaviours or actions that objects of
                    the class can perform.

                 Every class must have a Name and attributes, where data members and methods are encapsulated in the second bracket.
                    Class    :  Student_class9
                    Objects  :  Ajay, Shreyashi

                                                            Student_class9
                                             Data Members                  Methods
                                        Roll_No                   Academic activities ()
                                        Gender                    Extra-curricular activities ()
                 Using the class, two objects are created:

                                                                       Ajay
                                                   Data Members               Methods
                                                  Roll_No = 1       Academic activities () –
                                                                    first in studies
                                                  Gender = M        Extra-curricular activities () –
                                                                    play cricket


                                                         Shreyashi
                                       Data Members               Methods
                                      Roll_No = 2       Academic activities () –
                                                        Average in studies
                                      Gender = F        Extra-curricular activities () –
                                                        play badminton, participates in
                                                        drama and loves singing.



                                                                          Elementary Concept of Objects and Classes  43
   40   41   42   43   44   45   46   47   48   49   50