Monday, 30 September 2013

Class in Java

                 A class can be defined as a logical structure which contains data and code. To implement anything in Java you have to use class. The class defines a new type. With the help of class you can create objects of type class. You can create any number of objects of a class. 

                When you deal with class you will often listen these three words, instance-variables, object of class and methods of class. 

                Instance-variables are the variables declare within the class to hold the data. They are referred as instance-variable because each instances of class or objects of class will have their own copy of these variables (instance-variables). Method contains code or business logic. 

                  In Java, Class is created using the keyword class.

Below you can see the general syntax of class: 

class SampleClass
         type instance-variable1;
         type instance-variable2;
         type instance-variableN;

        SampleClass object1=new SampleClass();
        SampleClass object2=new SampleClass();
        ………………………………… // objects and its reference variable are created.
        SampleClass objectN=new SampleClass();
        type method-name1 (parameter1,parameter2,…)
              //body of method

          type method-nameN (parameter1,parameter2,…)
              //body of method

                   The variables defined within the class are called instance-variables. The codes or business logics are written inside the methods. The objects and its reference variables are created using the code fragment mentioned in the general syntax. i.e.

                     SampleClass object1=new SampleClass();

                    Where, object1 is the reference variable to the object which holds the reference value i.e. the address location of object. The new  keyword is used to allocate memory for the class object. The SampleClass() is the constructor of the class. 

                     The above fragment can also be rewritten as the below. 

                                 SampleClass object1;

                     This code fragment only declares the variable of type SampleClass. And the value of object1 is null at this point.
                                 object1=new SampleClass();

                      After the execution of this code fragment, object1 is dynamically assigned with the reference value i.e. address of memory location of object created.

                     Below picture is used to explain the above statements: 

Let us see a simple program example. Here we will create a class called Rectangle. And create objects of this class to study the behavior of objects.  

package sample.example1627;

public class Rectangle
   double length;
   double breadth;
   double Area()
               return length*breadth;

package sample.example1627;

public class WorkingRectangle
            public static void main(String[] args)
               Rectangle rectangle1=new Rectangle(); // object created
       Rectangle rectangle2=new Rectangle();  // object created
       rectangle1.length=50; //  initializing variable length
       rectangle1.breadth=60; //  initializing variable breadth
       rectangle2.length=100; //  initializing variable length
       rectangle2.breadth=200; //  initializing variable breadth
       System.out.println("Area of rectangle object1 "+rectangle1.Area());
       System.out.println("Area of rectangle object2 "+rectangle2.Area());

Area of rectangle object1 3000.0
Area of rectangle object2 20000.0

                      From the above program example it is clear that two different objects are created and each object will have its own copy of instance-variable. And no two objects will interfere with each others’ data.  In the above program example you can see that the value of area calculated by two different objects are different. 


                      In the above diagram, you can notice two objects of rectangle class are created and both are allocated space in two different memory locations and rectangle1 and rectangle2 are reference variables holding the address location of their respective objects. And each object created has its own length and breadth.

                      The class can also be defined as the blueprint from which you can create individual objects.     The visibility of class to other classes or code in same package or other package can be controlled by using access specifiers like public, protected, default.