0% found this document useful (0 votes)
4 views

Object

The document explains the Object class in Java, which is the root of the inheritance hierarchy and provides common behaviors to all Java objects. It details methods such as getClass(), hashCode(), equals(), clone(), notify(), and wait(), as well as the use of the clone() method for object cloning. Additionally, it discusses the 'this' keyword for referencing the current object and demonstrates its usage in constructors and method calls.

Uploaded by

nsrgperumal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Object

The document explains the Object class in Java, which is the root of the inheritance hierarchy and provides common behaviors to all Java objects. It details methods such as getClass(), hashCode(), equals(), clone(), notify(), and wait(), as well as the use of the clone() method for object cloning. Additionally, it discusses the 'this' keyword for referencing the current object and demonstrates its usage in constructors and method calls.

Uploaded by

nsrgperumal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

OBJECT CLASS

• Object class is present in java.lang package.


• Every class in Java is directly or indirectly
derived from the Object class.
• If a Class does not extend any other class then
it is direct child class of Object
• If extends other class then it is an indirectly
derived.
• Therefore the Object class methods are
available to all Java classes.
• Hence Object class acts as a root of
inheritance hierarchy in any Java Program.
• The Object class is the parent class of all the
classes in java by default.
• In other words, it is the topmost class of java.
• The Object class is beneficial if you want to
refer any object whose type you don't know.
• Notice that parent class reference variable can
refer the child class object, know as upcasting.
• The Object class provides some common
behaviours to all the objects
– object can be compared
– object can be cloned
– object can be notified etc.
returns the Class class object of
public final Class this object. The Class class can
getClass() further be used to get the
metadata of this class.

public int hashCode() returns the hashcode number for


this object.
public boolean equals(Object compares the given object to this
obj) object.
protected Object clone()
throws creates and returns the exact copy
CloneNotSupportedException (clone) of this object.

returns the string representation


public String toString()
of this object.
wakes up single thread, waiting on
public final void notify()
this object's monitor.
wakes up all the threads, waiting on this
public final void notifyAll() object's monitor.

causes the current thread to wait for the


public final void wait(long specified milliseconds, until another
timeout)throws InterruptedException thread notifies (invokes notify() or
notifyAll() method).

causes the current thread to wait for the


public final void wait(long timeout,int specified milliseconds and nanoseconds,
nanos)throws InterruptedException until another thread notifies (invokes
notify() or notifyAll() method).

public final void wait()throws causes the current thread to wait, until
InterruptedException another thread notifies (invokes notify()
or notifyAll() method).

is invoked by the garbage collector


protected void finalize()throws
before object is being garbage
Throwable
collected.
public class Student
{
static int last_roll = 100;
int roll_no;

Student()
{
roll_no = last_roll;
last_roll++;
}

// Overriding hashCode()
@Override
public int hashCode()
{
return roll_no;
}

public static void main(String args[])


{
Student s = new Student();

System.out.println (s.hashCode());
System.out.println (s.toString());
public class Test
{
public static void main(String[] args)
{
Object obj = new String(“HelloWorld");
Class c = obj.getClass();
System.out.println("Class of Object obj is : “ + c.getName());
}
}

Class of Object obj is : java.lang.String


Object Cloning in Java
• a way to create exact copy of an object.
• The clone() method of Object class is used to
clone an object.
• The java.lang.Cloneable interface must be
implemented by the class whose object clone
we want to create.
• If we don't implement Cloneable interface,
clone() method generates
CloneNotSupportedException.
Why use clone() method ?
• The clone() method saves the extra processing
task for creating the exact copy of an object.
• using the new keyword - take a lot of
processing time to be performed
class Student implements
Cloneable
{
int rollno;
String name;
Student (int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}

public Object clone()throws CloneNotSupportedException


{
return super.clone();
}
public static void main(String
args[])
{

try{
this keyword in java
• this is a reference variable that refers to the
current object.
• used to refer current class instance variable.
• used to invoke current class method
(implicitly)
• this() can be used to invoke current class
constructor.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}

class TestThis{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
class A{
void m()
{
System.out.println("hello m");
}
void n()
{
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
class TestThis{
public static void main(String args[])
{
A a=new A();
a.n();
}
}
class A{
A()
{
System.out.println("hello a");
}
A(int x)
{
this();
System.out.println(x);
}
}
class TestThis{
public static void main(String args[])
{
A a=new A(10);
}
}

You might also like