Wednesday, 28 August 2013

Object class

Object class is the super /parent class of all the classes in the java.
if you don't know the type of any class just pass it to the object class ,which performs the up casting.


Example:-

in the above example we print the reference variable obj.which refers the object.object stored in a memory location in hexadecimal form.

Methods of Object class:-

public String toString():-returns the string representation of the object.

Example:-


in this example we override the toString() method of the Object class and return the value of i.
System.out.println(obj); bydedault toString() method present in this......
it is also written as  System.out.println(obj.toString()); .so by printing the reference variable we get the value of i which is initialize through the constructor.

Example 2:-




in this we take 2 parameters.

public boolean equals(Object obj):-it is used to compare 2 reference variables and return type is boolean if equal return true otherwise false.

Example :-


it compares on the basis of memory address not on the basis of value passed in constructor.

public int hashCode();- return hashCode number for the object.

Example:-


it returns the address in the integer form.

Used all the methods in single program.
Example:-


in the above example we override all the methods.
you pass the argument at the time of object creation which is initialized by the constructor and we return that i value from all the overrided method.in this it works on the value not on the reference variable.
 in the equals we use downcasting for converting Object type into Z type

instanceof ():- is used to check either it is reference variable of the object or not.
These are the basic methods uesd in the object class.

Some of the members are as follows:-

  • protected Object clone()
  • public final void notify()
  • public final void notifyall()
  • wait()







No comments :

Post a Comment