Sunday, February 15, 2009

Comparable Interface and clonable interface

Comparable Interface:
This is present in java.lang.package
Contains the following one method.
1.public int compareTo(Object o)
if returns –ve integer if o1 has to place before o2.
If returns +ve integer if o1 has to to place after o2.
If returns zero then o1 and o2 are equal.
All the wrapper classes and string class already implemented comparable interface. But the StringBuffer doesn’t implement comparable interface.
Comparable Interface Example:
Interface comparable
{
public int compareTo(Object o)
{
TreeSet t=new TreeSet(0;
t.add(“a”);
t.add(‘z”);
System.out.println(t); // a,z (:: z.compareTo(“a”);)
System.out.println((“a”).compareTo(“z”)); //-25
System.out.println((new Integer(10).compareTo(new Intege(1)); // +1
}
}|
Marker or Tag Interface:
If an interface is marked for some ability such type of interfaces are called Marker interfaces.
Ex: clonable,serializable
If an interface with out any method obviously accept ass marker interface.
Eventhough interface contains some methods,still we can consider as marker interface .If it is marked for some ability.
Ex:Comparable interface.
Intreger i1=new Integer(10);
Integer i2=new Integer(20);
System.out.println(i2.compareTo i1); // -1
System.out.println(i1.compareTo i2); // +1
System.out.println(i1.compareTo i1); // 0
System.out.println(i1.compareTo(“a”)); // CTE nullpointer exception


Cloneable interface:
Uptaining exact copy of a plant ,a bird,an animal or a human being is called cloning.
Cloning in programming uptaining bit wist exact copy of an object is called cloning.
cloning Example:

Class sample() implements cloneable
{
Int i=10;
Public static void main(string args[])throwsClone notSupportedEexception
{
Sample s1=new sample();
Sample s2=s1;
Sample s3=(sample)s1.clone();
S1.i=1000;
System.out.println(s3.i);
System.out.println(s1==s3);
we should type cast otherwise
CTE:in compatable types
Found: Object reqired=sample
The class must implements cloneable interface otherwise at runtime clone() results cloneNotsupportException
Example:
Class sample implements clonable
{
Int i=10;
Public static void main(string[]args)throws cloneNot support Exception
{
Object o=new object();
Object o2=o.clone();
}
CTE:clone() has protected access in java.lang.object
The protected numbers we can access, from with in the same package or from outside package but from outside package,the protected number can be accessed b using child class reference only.ie we can’t use parentclass reference to access protected number from outside package,validation leadsto CTE.

clone() method:
Protected object clone() throws clone not supported exception.
This method can used to prouduced exactly duplicate copy of an object..
All the objects can’t produce cloned object ,only clonable objects can produce duplicate copies .
An object is said to be conable if and only if the corresponding class implements clonable interface.
By using the folling object class clone() method we can produced cloned objects
Protected object clone() throws clone supported exception
CheckedException so we should handle by using try catch or throws to the caller by using throws clause.

Shallow cloning Example:
Class Student implements cloneable
{
String name;
String age;
Student(String name,String age)
{
This.name;
This.age=age;
]
Public Object clone()throws cloneNotSupportedException
{
Return this;
}
}
Class Student cloneDemo
{
Student s1= new Student(“hai”,”22”);
Student s2= (Student)s1.clone();
S2.name=”abc”;
System.out.println(s1.name);
Public static void main (String ar[])
{
StudentCloneDemo s1= new Student cloneDemo();
}
}
Deep cloning example:
Class Student implements cloneable
{
String name;
String age;
}
Student(String name ,String age)
{
This.name=name;
This.age=age;
}

Public Object clone(0throws CloneNotSuport Exception
{
try
{
ByteArrayOutputStream bas=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bas);
Oos.writeObject(this);
ByteArrayInputStream bias=new ByteArrayInputStream(bas.toByteArray());
ObjectInputStream oos=new ObjectInputStream(bias);
Return ois.readObject();
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
class DcloneDemo
{
DcloneDemo()throws CloneNotSuportException
{
Student s1=new Student(“hello”,”200”);
Student s2=(Student)s1.clone();
S2.name=”java”;
System.out.println(s1.name);
Public static void main(String[] arg)
{
new DcloneDemo();
}
}



1 comments:

karthik said...

by implementing comparable interface what extra ability the object is geting

Post a Comment