x

Java – Object Class

PREV     NEXT

  • Object class is the super class of all the other classes in java.
  • Every class is a subclass of the object class and hence they can use or override the methods present in the Object class.
  • Since subclass objects can refer all the elements of parent class, all classes defined in java program can access methods and variables of Object class.

Example:

Object ob = employee;


Object ob = getDetails();              //getDetails() may return object of any type

int names[] = new int(10);

Object obj = names;                       //includes arrays

Methods of Object class:

1.

Method: 

public final ClassgetClass()

Description: 

Can be used to get the runtime class of the object. Also used to get the other information about the class Copies an array from the source array, beginning at the specified position, to the specified position of the destination array.

length – the number of array elements to be copied.

2.

Method: 

public int hashCode()

Description: 

Returns a hash code value computed for the object.

3.

Method: 

public boolean equals(Object obj)

Description: 

Returns true, if some object is equal to the current object. Otherwise, returns false.

This is most used when comparing two different objects. By default, two objects are considered equal when their hashcodes are same. Otherwise, programmer must override this equals method to clearly define on which basis the objects should be compared.


4.

Method: 

protected Object clone()

Description: 

Creates and returns a copy of this object.

5

Method: 

public String toString()

Description: 

Returns the string representation of the object.

6

Method: 

public final void notify()

Description: 

Wakes up a thread, which is waiting on this objects’s monitor.

7

Method: 

public final void notifyAll()

Description: 

Wakes up all the threads, which are waiting on this objects’s monitor.

8

Method: 

public final void wait(long timeout)

public final void wait(long timeout, int nanos)

public final void wait()

Description: 

 Causes the current thread to wait for the time if any specified, until  another thread invokes the notify() method

9

Method: 

protected void finalize()

Description: 

Used by the garbage collector, when it learns that there are no more reference to the current object. Can be overridden by any subclass to perform the cleanup process.

PREV     NEXT



Like it? Please Spread the word!