Object class in java is available under java.lang package and all classes are directly or indirectly sub claases of this Object class. Hence we can call Object class as super class of all java classes.
Object class has below specified methods. It is not needed to use these methods but if we want to use we can override all these methods with our class specific code.
1. protected object clone(): It will create copy of object and returns it. It will throw CloneNotSupportedException.
2. boolean equals(Object obj) : it will check some other object is equal to this object or not.
3. protected void finalize() : This method will be called by Garbage collector when it identify no references for the object.
4. public final class getClass() : it will return the runtime class of object.
5. public int hashcode() : it will return the hascode value of a given object.
6. public String toString() : It will returns the string representation of object
And also we have below methods available regarding the synchronization:
7. public final void notify() --- it will notify/wake up the waiting thread.
8. public final void notifyAll() --- it will notify/wake up all waiting threads.
9. public final void wait() --- it will make current thread to wait. until either notify or notify all method is invoked by other thread for this object
10. public final void wait(long timeout) --- it will make current thread to wait. until either notify or notify all method is invoked by other thread for this object or specified amount of time was elapsed
11. public final void wait(long timeout, int nanos) --- it will make current thread to wait. until either notify or notify all method is invoked by other thread for this object or some other thread interruptsce current thread or specified amount of time was elapsed
Sample java program:
package com.sample.java.testing;
public class ObjectClassTest implements Cloneable{
public void getMessage(){
System.out.println("hi wlecome to java world");
}
// This class should implement the Cloneable
// interface to clone this object otherwise if we try to call this clone method, we will get
// java.lang.CloneNotSupportedException
//If we implement cloneable interfcae, then we can clone this object
public ObjectClassTest clone() throws CloneNotSupportedException{
ObjectClassTest newAction = (ObjectClassTest)super.clone();
return newAction;
}
}
package com.sample.java.testing;
public class ObjectClassTestDemo {
public static void main(String[] args) throws CloneNotSupportedException {
ObjectClassTest classTest = new ObjectClassTest();
ObjectClassTest classTest2 = classTest.clone();//clone
classTest2.getMessage();
System.out.println(classTest.hashCode());//hashcode
System.out.println(classTest.equals(classTest2));//equals
System.out.println(classTest2.toString());//to string
System.out.println(classTest);
}
}
Output:
Exception in thread "main" java.lang.CloneNotSupportedException: com.sample.java.testing.ObjectClassTest
at java.lang.Object.clone(Native Method)
at com.sample.java.testing.ObjectClassTest.clone(ObjectClassTest.java:11)
at com.sample.java.testing.ObjectClassTestDemo.main(ObjectClassTestDemo.java:8)
If we implement the cloneable interface, we will get output as below
hi wlecome to java world
29676948
false
com.sample.java.testing.ObjectClassTest@1a21389
com.sample.java.testing.ObjectClassTest@1c4d594
Object class has below specified methods. It is not needed to use these methods but if we want to use we can override all these methods with our class specific code.
1. protected object clone(): It will create copy of object and returns it. It will throw CloneNotSupportedException.
2. boolean equals(Object obj) : it will check some other object is equal to this object or not.
3. protected void finalize() : This method will be called by Garbage collector when it identify no references for the object.
4. public final class getClass() : it will return the runtime class of object.
5. public int hashcode() : it will return the hascode value of a given object.
6. public String toString() : It will returns the string representation of object
And also we have below methods available regarding the synchronization:
7. public final void notify() --- it will notify/wake up the waiting thread.
8. public final void notifyAll() --- it will notify/wake up all waiting threads.
9. public final void wait() --- it will make current thread to wait. until either notify or notify all method is invoked by other thread for this object
10. public final void wait(long timeout) --- it will make current thread to wait. until either notify or notify all method is invoked by other thread for this object or specified amount of time was elapsed
11. public final void wait(long timeout, int nanos) --- it will make current thread to wait. until either notify or notify all method is invoked by other thread for this object or some other thread interruptsce current thread or specified amount of time was elapsed
Sample java program:
package com.sample.java.testing;
public class ObjectClassTest implements Cloneable{
public void getMessage(){
System.out.println("hi wlecome to java world");
}
// This class should implement the Cloneable
// interface to clone this object otherwise if we try to call this clone method, we will get
// java.lang.CloneNotSupportedException
//If we implement cloneable interfcae, then we can clone this object
public ObjectClassTest clone() throws CloneNotSupportedException{
ObjectClassTest newAction = (ObjectClassTest)super.clone();
return newAction;
}
}
package com.sample.java.testing;
public class ObjectClassTestDemo {
public static void main(String[] args) throws CloneNotSupportedException {
ObjectClassTest classTest = new ObjectClassTest();
ObjectClassTest classTest2 = classTest.clone();//clone
classTest2.getMessage();
System.out.println(classTest.hashCode());//hashcode
System.out.println(classTest.equals(classTest2));//equals
System.out.println(classTest2.toString());//to string
System.out.println(classTest);
}
}
Output:
Exception in thread "main" java.lang.CloneNotSupportedException: com.sample.java.testing.ObjectClassTest
at java.lang.Object.clone(Native Method)
at com.sample.java.testing.ObjectClassTest.clone(ObjectClassTest.java:11)
at com.sample.java.testing.ObjectClassTestDemo.main(ObjectClassTestDemo.java:8)
If we implement the cloneable interface, we will get output as below
hi wlecome to java world
29676948
false
com.sample.java.testing.ObjectClassTest@1a21389
com.sample.java.testing.ObjectClassTest@1c4d594
No comments:
Post a Comment