Monday 13 February 2012

What is Class And Object in JAVA ?

 Class :-
   Class is Blue Print [I hope you will not ask me what is mean by blue print.. if you don't know then just Google it..] or you can say it is a template for creating an object.
  
 For EX, Now lets create a "Bird" class , So what are things needed to create a 'Bird' class think.... 

  Finished thinking .... ok lets go to subject

 So for Bird Class we need Color,Name,CanFly,Size and so on... 

Now let us create a blue print for the class Bird :

 public class Bird {
                 String name ;
              String color;
           char size;
}
    So now we have a bird class which is a blue print for any kind of Bird Object  which we create ; means ,when we create a Bird object which will have name,color , size[what ever bird it may be].
   
    So once again ,when ever we create a bird object it will have all the state[name,color,size]. Technically the state[name,color,size] are called as instance variables.
   Now let us create a object of this bird class and see how it works.
   public class CreateBirdObjectAndRun {
       public static void main(String[] args) {
        Bird bird = new Bird(); //Here is the Bird Object
        bird.name ="Pigeon";
        bird.canFly = true;
        bird.color ="White";
       bird.size = 'S'; //Here S - Small ,
       
       System.out.println(bird.name);// Here bird object reference.object state[here name], so without object we cannot do anything with only class
       System.out.println(bird.canFly);Here bird object reference.object state[here canFly]
       System.out.println(bird.color);Here bird object reference.object state[here color]
      System.out.println(bird.size);Here bird object reference.object state[here size]
    }
}
 
   The below Diagram show you What is happening with the Bird bird = new Bird() Object

    




1 - Shows the Complete Details of the line Bird bird = new Bird();

2 - 'Bird' is used for creating the space in JVM[Java Virtual Machine].

3. 'new Bird()' creates a New Bird object in  JVM.

4. The 'bird' is now pointing to the Bird object in the JVM. In simple words bird is the reference which holds the reference[memory address] of the Bird Object in JVM.

     We Can create how many objects we want by using the same procedure because we have the Blue Print[Bird class] From which we can create many number of birds like peacock,ostrich,parrot and so on..

    So class are the Blue print from which any number of  objects can be created and used ...

So We have seen that Class is the blue print For creating objects.So now what is Objects?

object:
    Object is nothing but the Instance[one which really exist of particular type] of Class.

  [In general English  Instance means a specific term and the one which exist really for eg ;  The last instance = Here last which tells last occurrence which is specific and and the one which really exist (Don't confuse instance without 'example' ;, example - it may be reality or non-reality but instance means which exist really)]



No comments:

Post a Comment