Thursday, 18 June 2015

Object Class

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

Methods in Object class

  1. clone()
  2. equals(Object obj)
  3. finalize()
  4. hashCode()
  5. notify()
  6. notifyAll()
  7. wait()
  8. wait(long timeout)
  9. wait(long timeout, int nanos)


protected  Object clone()
          Creates and returns a copy of this object.
 boolean equals(Object obj)
          Indicates whether some other object is "equal to" this one.
protected  void finalize()
          Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
 Class getClass()
          Returns the runtime class of an object.
 int hashCode()
          Returns a hash code value for the object.
 void notify()
          Wakes up a single thread that is waiting on this object's monitor.
 void notifyAll()
          Wakes up all threads that are waiting on this object's monitor.
 String toString()
          Returns a string representation of the object.
 void wait()
          Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
 void wait(long timeout)
          Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
 void wait(long timeout, int nanos)
          Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
toString ()
If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Advantage of the toString() method

By overriding the toString() method of the Object class, we can return values of the object, so we don't need to write much code.

Understanding problem without toString() method

Let's see the simple code that prints reference.
1.    class Student{  
2.     int rollno;  
3.     String name;  
4.     String city;  
5.      
6.     Student(int rollno, String name, String city){  
7.     this.rollno=rollno;  
8.     this.name=name;  
9.     this.city=city;  
10.  }  
11.   
12.  public static void main(String args[]){  
13.    Student s1=new Student(101,"Raj","lucknow");  
14.    Student s2=new Student(102,"Vijay","ghaziabad");  
15.      
16.    System.out.println(s1);//compiler writes here s1.toString()  
17.    System.out.println(s2);//compiler writes here s2.toString()  
18.  }  
19. }  
Output:Student@1fee6fc
       Student@1eed786
As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let's understand it with the example given below:

Example of toString() method

Now let's see the real example of toString() method.
1.    class Student{  
2.     int rollno;  
3.     String name;  
4.     String city;  
5.      
6.     Student(int rollno, String name, String city){  
7.     this.rollno=rollno;  
8.     this.name=name;  
9.     this.city=city;  
10.  }  
11.    
12.  public String toString(){//overriding the toString() method  
13.   return rollno+" "+name+" "+city;  
14.  }  
15.  public static void main(String args[]){  
16.    Student s1=new Student(101,"Raj","lucknow");  
17.    Student s2=new Student(102,"Vijay","ghaziabad");  
18.      
19.    System.out.println(s1);//compiler writes here s1.toString()  
20.    System.out.println(s2);//compiler writes here s2.toString()  
21.  }  
22. }  
Output:101 Raj lucknow
       102 Vijay ghaziabad


No comments:

Post a Comment