Page 131 - Computer science 868 Class 12
P. 131

We can also say “an object is an instance of a class”:
                 As soon as an object is created in object-oriented language, it acquires memory in RAM but this does not happen while
                 defining a class. Thus, objects are the physical existence of the class.

                 We know data types such as int, long, float, etc., are predefined classes, similarly, user-defined classes create objects
                 that contain all the properties and methods of those classes, and also acquire memory and an Object represents a
                 specific state of the class.

                 Thus, we can say Object is an instance of a class.
                 In the above program, the class Student has 2 objects (st1 and st2). Both objects acquire memory of the same size and
                 have the same properties and methods.
                 So, we can say that “Object is an instance of a class”.

                 We know that there are two types of data types. They are Primitive Data Types and User-defined data types. Primitive
                 data types are created by Java Developers and are simply used in programs. User-defined data types are a combination
                 of primitive data types and are also referred to as derived data types.
                 Class in  OOP is made up of attributes that are a combination of primitive data types. And when we create an object, it
                 contains all these attributes in the form of instance variables. So, we can say Class is a user-defined data type.

                 4.3.3 Methods
                 To perform a specific function or job as required, Java uses a block of codes that may be reused as and when required.
                 These blocks of codes are known as Methods. It is required to break the complex programs into smaller codes so it
                 becomes easy for debugging.
                 For example,
                    void sum(int a, int b)
                    {
                        int s=a + b;
                        System.out.println(s);
                    }
                 To call the method, the following syntax is needed:
                    objectname.methodname(parameter list);
                 For example, to call the above method,
                    ob.sum(5,6);

                 4.3.4 Instances
                 Each object has attributes which are created as soon as the object is created. These attributes are known as instance
                 variables. Thus, we can see every object has its own copy of the instance variables. Instance variables can be of any
                 data type like int, float, double, etc., also it can be of user-defined data types.

                 Syntax of instance variables
                    class <class_name>
                    {
                        declaring instance variables;
                        ....
                    }
                 For example,

                    class sum
                    {
                        int a,b,s;
                        ...
                    }


                                                                                                                       129
                                                                                                   Programming in Java  129
   126   127   128   129   130   131   132   133   134   135   136