Fallowing icon

Saturday, September 29, 2012

Objects in java


object


        An object is an abstract representation of a thing in the real world.We can find many objects around us, home, computer, car, phone etc. Those objects have got states and behaviors. An object have unique identity, attributes and behavior. 

       When we create car object it's states are door,color, tier, current speed, engine and seat. It's behaviors are driving speed, parking, applying brakes and changing gear.

       The software objects are similar to the real world objects. In that case software objects very familiar to us. We use new keyword for create objects in java. 



Create Objects


       We already identify car method and it's states and behaviors. Now we build real car is using this states and behaviors, We say it object. Every time we create object using class.  

There are three steps when creating an object from a class-

  • Declaration   - First create variable is used an object type.

  • Instantiation - Using the new key word create the object

  • Initialization- The new keyword is followed by a call to a constructor and initializes the new object.


 Discussion-   
  



 
     
      
We have discussed what are the states and behaviors. 

Data Type(Car) -
        
        We discussed the data types in third lesson. We create new object is using new keyword.
        We divide all data types in to patterns, one is primitive and other one is reference. In this example, car data type come with under the reference data type. The a variable is catch our new object memory address. 


Example-

           public class Test { 
    
                    public static void main(String[] args) {
                           Test a=new Test();
        
                          System.out.println("Value of a variable is ="+a);
                  }
            } 


Copy this code and try. You can see print some numbers and shambles. It's memory address. 



 When you create method in your computer, it's memory consumed. This Car a memory variable(reference(Object) type variable) is create on memory Stack area and states and behaviors are create on memory Heap area. 


Example- 

                class Car { 
    
                           int speed;                                                            //declare the speed variable

                           void accelerateCar (){                                       //declare the methods
                           speed+=10;
                           System.out.println("Speed is ="+speed);
                           }
                           void breakCar(){
                           speed=0;
                           System.out.println("Speed is ="+speed);
                            }
    
                           public static void main(String[] args) {
                                        Test a=new Test();                                      //declare the object

                                        System.out.println("push accelerater");
                                        a.accelerateCar();                                      //call the accelerateCar method
                                        System.out.println("push accelerater");
                                        a.accelerateCar();
                                        System.out.println("push break");
                                        a.breakCar();                                              //call the breakCar method
                                        System.out.println("push accelerater");
                                        a.accelerateCar();
                                        System.out.println("push accelerater");
                                        a.accelerateCar();
                                        System.out.println("push accelerater");
                                        a.accelerateCar();        
                           }
                  }







   


  First we create speed  variable, then we create accelerateCar method. Now we addition 10 for speed variable and print is using print command in this method. 
  Then create breakCar method and assign 0 to speed variable
  Now we create object and first call accelerateCar method is using a.accelerateCar. Now execute eccelerateCar method and add 10 for speed variable. then call again eccelerateCar method, Now speed is 20. Because speed+=10; mean is speed =speed+10; when you call this method, add 10 for speed variable. then we call breakCar method, now speed variable is assign to 0. now show speed is 0. then call accelerateCar method is three time. In this case,fianal seep is equal to 30.


No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment