Thursday 24 October 2013

Session 6:- Object & Class

         Object:-

  • Object is real world entity which has States & Behavior.
Ex: Pen,Mobile,Person,Book,Car,Tv,College,Computer,fan,Bus,Company..etc
  •  States generally represent property of object.
  •  Behavior represents an action or functionality.
  •  When we Create an object, we required
    • Plan or master copy, it's called Class.
    •  Plan or Class contains states and behavior which will share with another object.
  • Any number of objects created in class.
  •  The entire object works independently. 
  • When object is modified or destroyed it may not affect in another object.
  •  Consider this women is one object so  It has states and behavior.

    (States)    (Behavior)
    -lags       -playing    
    -cap        -walk        
    -hands     -talk    
    -stick      -stand  
    -eyes       -etc   
    -etc
    • States of class can be called as instance variable.
    • Behavior of class can be called as instance method.
    • In general is called as instance member of class because these members are shared with multiple instances.
    • Sometime the instance variable will be initializing to a value or data and then variable value is same in the all instance.
    • If We don’t initialize instance variable than the instance variable value are different for different object.but when we create the object at that time its take default value for that variable.Default value is depend on the data type if instance variable data type int,byte,short,long than its take 0. for double and flout 0.0.
    • for boolean false
    • for String null
    •  In java the object created in heap memory.
    •  The allocation memory will be taken care by JVM.
    •  JVM is allocation new memory when we use keyword new. 

    Syntax:- Object

    Class-name variable name= new Class-name();
      Ex: Fan f =new Fan ();
      •  Left side key word new Fan(); is the object of the Fan class.
      • f is the reference variable.
      •  Fan is data type of the object.
    •   To access multiple member of an object we required reference variable.
    •   Reference variable data type must be same as object type.
                
                         Tree t = new Tree ();        /*compare these two statement
                          int   i = 20;                       Tree and int is data type t and i variable and  
                                                                    After = sign is value.*/
    Syntax:- Class

                class class-name
                {
                    // instance variable (state)
                    // instance method (behavior)
                }
    NOTE:- Class name Start with capital latter(industry standard).

    Ex: class Student      //
    {
       // instance variable 
       String name;
       int rollno;
     //  instance method
    public void  reading()
       {
            System.out.println("Reading");
        }                                   

    •   Class can also be treated as a custom data type.
    •   Reference variable always categories of non-primitive variable.
    •   Any number of reference variable can be refer a single object.
    Ex:
                       L.H.S              R.H.S
                 Fan     f       =    new        Fan();


    •  new Fan(); is object of class Fan.
    •   new keyword is use for create the object of class and initialize heap memory. 
    •  f is reference variable.(non-primitive variable).
    •   Fan is data type of object. Data type is same as the object name.
    •  When a new object is created that object will be shared with the entire instance variable and these instance variables are initialize to default value.
    •  Default values of an instance variable depend on data type of the variable.
    •  Non-primitive instance variable are initialize to NULL.
    •   Primitive instance variable are initialize to appropriate respective value.
    •   Calling a method by passing a primitive value is called as call by value. 
    •   Calling a method by passing an object reference or reference variable is called call by reference.  
  •  Example  :- Call by value
    public class Calculator{
    public void  addNo(int n1,int n2)
    {    
    int n3;
     n3=n1+n2;
    System.out.println (n3);
    }
                  }
    public class Math{
    public static void main(String args[])
    {
    // 1st  way to run this program
    new Calculator().addNo(20,30);
    /* 2nd  way to call this method
    Calculator c=new Calculator ();
    c.addNo(10, 20);
     
  • Example  :- Call by Reference 

    public class Calculator {
    int a=12;
    nt b=34;
    public void  addNo(Calculator c)
    {
     int n3;
     n3=c.a+c.b;
    System.out.println (n3);
    }               
                  }
    public class Math{
    public static void main(String args[])
    {
    Calculator c=new Calculator ();
    c. addNo(c);

    /* when we use new keyword then object is created in heap memory and memory will be initialize. Here we passing the Calculator class object or reference so it called call by reference */

    }

No comments:

Post a Comment