Adobe Interview Question for Java Developers


Country: India




Comment hidden because of low score. Click to expand.
1
of 1 vote

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("K");
			b.setSurname("P");
		
			B bClone = (B)b.clone();
			System.out.println(bClone.getName());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}

- Kris May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
{
}

- danqianchen April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the class Animal is in a lib and it is not serializable, and of course you can not update also.

- Manish April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 May 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
		}
	}

}

- Kris May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- ankit May 08, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More