Adobe Interview Question
Java DevelopersCountry: India
public Dog deepCopy()
{
Dog toReturn = null;
try {
Dog dog = new Dog();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(dog);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
Object obj = in.readObject();
toReturn = (Dog)obj;
}
catch(IOException e) {
e.printStackTrace();
}
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return toReturn;
}
class Animal implements Serializable
{
}
class Dog extends Animal implements Serializable
{
}
even if Animal is Serializable it can have transient fields, so deserialized instance will have those fields set to 0. So the "deep" copy will not match original with serialization approach.
The whole hierarchy should support deep cloning - all of the super classes support deep clone through Clonable interface of a designated interface with a method like deepClone.
A.java
----------------------------------------------------------------------------------------
public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
A a = new A();
a.setName(this.getName());
return a;
}
}
B.java
-------------------------------------------------------------------------------------------
public class B extends A{
private String surname;
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
protected Object clone() throws CloneNotSupportedException {
A a = (A)super.clone();
B b = new B();
b.setName(a.getName());
b.setSurname(this.getSurname());
return b;
}
public static void main(String[] args) throws CloneNotSupportedException
{
try
{
B b = new B();
b.setName("Krishna");
b.setSurname("Patni");
B bClone = (B)b.clone();
System.out.println(bClone.getName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
hi , is there any need for all that , if we just implement Clonable marker interface in Dog class , it ought to work. deep cloning is only tricky when external objects are involved. for example
{
public class Dog extends Animal implements Cloneable{
private String name;
public Dog(String name,String type) {
super(type);
this.name = name;
}
public void print(){
System.out.println("name is "+name+" type is "+super.type);
}
public static void main(String[] args) {
Dog dog = new Dog("lab","mammal");
dog.print();
try {
Dog clone = (Dog)dog.clone();
System.out.println(dog.clone() instanceof Object);
clone.print();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Animal {
transient protected String type;
public Animal(String type) {
super();
this.type = type;
}
}
}
output returned is
name is lab type is mammal
true
name is lab type is mammal
- Kris May 03, 2012