Object Class- Java

The Object class is the parent class/super class of all the classes in java by default. In other words, it is the topmost class of java. It is also called as universal data type.

Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object.
It has several methods they are

Method

Description

public final Class getClass()

returns the Class class object of this object. The Class can further be used to get the metadata of this class.

public int hashCode()

returns the hashcode number for this object.

public boolean equals(Object obj)

compares the given object to this object.

protected Object clone()

creates and returns the exact copy (clone) of this object.

public String toString()

returns the string representation of this object.

public final void notify()

wakes up single thread, waiting on this object's monitor.

public final void notifyAll()

wakes up all the threads, waiting on this object's monitor.

public final void wait(long timeout)

causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method).

protected void finalize()

is invoked by the garbage collector before object is being garbage collected.

 Program on Boxing and Un boxing

class obj1
{

            public static void main(String[] args)
{

int a=10;
Object p;
p=a; // Boxing
int k=(int) p; // unboxing
System.out.println(k);

}

}

Find the factorial value using boxing and un-boxing

class factorial
{

            public static int fact(Object x)
{
int f=1,i;
for(i=1;i<=(int)x;i++)
{
f=f*i;
}
return f;
}
public static void main(String[] args) {

int a;
Scanner r=new Scanner(System.in);
System.out.println("Etner no");
a=r.nextInt();
System.out.println("Factorail value "+fact(a));


}

}

Program to find the sum of 2nos by return object class type

import java.util.Scanner;

public class adition
{

            public static Object sum(Object x,Object y)
{
Object k;
k=(int)x+(int)y;
return k;
}
public static void main(String[] args)
{

int a,b,c;
Scanner r=new Scanner(System.in);
System.out.println("Etner 2 no");
a=r.nextInt();
b=r.nextInt();
c=(int)sum(a,b);
System.out.println("sum of 2nos "+c);

}

}

 

For
Online Classes

Contact Us: 9885348743