javascript1

Thursday, 12 February 2015

class and objects in Java

Object in Java

object in java
object basically define the state  and  behavior  .
example like banking etc .
object is instance of class 
like if you consider human being then it`s state is name , colour , residence and behavior is running , sleeping , working .


class and objects
Class  is a template or blueprint  which define the structure .

Class have no existence until objects are not created because objects defines the state of class .
A class in java include

  • data member
  • method
  • constructor
  • block
  • class and interface
Data member
Data member is a member which is declared in a particular class .  it`s treated as variable in class
Eg 
class MyjavaProgram()
{
Public int a; // data member

}
Method
Method is member functions of class . it is like function in c .
Constructor
Constructor  is a special member function whose name is same as class name . 
Constructor are invoked without creating any object .
Class and interface
A class can also contain other calss and interface  it is known as nested class

Creating an Object:

Class  is a template or blueprint  which define the structure .
Class have no existence until objects are not created because objects defines the state of class so we have to create class object .
 for creating a object three main steps are required :
1.     Declaration: object must  be defined before using it by using class name  .
2.     Instantiation: in java  new' keyword is used to create the object.
3.     Initialization: in java if there is no custructor in class then default constructor is invoked which  Initialization all the data members .


 Example define the process of  creating object :

public class Hello{
 
   public Hello(String name){
      // This is constructor of classwith  one parameter, name.
      System.out.println("welcome your name is  :" + name ); 
   }
   public static void main(String []args){
      // given statement  create an object obj that will call Hello constuctor
      Hello obj = new Hello( "vinay" );
   }
}
after  compilation and run the above program  it would give result:
Wecome your name is  :vinay 

 Example of class , mehod  , data member in java program
     class StudentRecord{  
        int id;  
        String name;  
  
            void getData(int a, String b){  //method  of class  
             id=a;  
             name=b;  
              }  
  
          void display(){System.out.println(rollno+" "+name);}//method  of class
  
           public static void main(String args[]){  
           StudentRecord obj1=new StudentRecord();  
           StudentRecord obj2=new StudentRecord();  
  
           obj1.getData(111,"Karan");  
           obj2.getData(222,"Aryan");  
  
           obj1.display();  
           obj2.display();  
  
            }  




Output:111 Karan
       222 Aryan
    





As you see in the above figure, object gets the memory in Heap area and reference variable refers to the object allocated in the Heap memory area. Here, s1 and s2 both are reference variables that refer to the objects allocated in memory.


No comments:

Post a Comment