Anaplan Interview Question
1. Class A has variable a, Class B extends from A which has b, Class C extends from Class B which has c. Set values for those variables. Write a clone method which returns a deep copy. For example
ClassA a = new ClassC(5); which will print 5,4,3 set by constructors in those classes.
ClassA x = a.myClone();
x.print() should also print 5,4,3.
Interview Answers
While I understand what he wants I wasn't able to answer it. I got as far as cloning two levels.
My issues with this question is it's very academic. I gave this question to most of my colleagues(some are architects with 30 years exp) and no one can answer it. Not sure how the interviewer is trying to calibrate me based on this question.
class A
{
protected int a;
public A(int a)
{
this.a = a;
}
protected void print()
{
System.out.println(a);
}
public A Clone()
{
return new A(a);
}
}
class B extends A
{
protected int b;
public B(int b)
{
super(b);
this.b = b - 1;
}
protected void print()
{
super.print();
System.out.println(b);
}
public A Clone()
{
return new B(b);
}
}
class C extends B
{
private int c;
public C(int c)
{
super(c);
this.c = c - 2;
}
protected void print()
{
super.print();
System.out.println(c);
}
public A Clone()
{
C newobj = new C(a);
return newobj;
}
}
public class Singleton
{
int[] weight = { 1, 2, 4, 2, 5 };
int[] value = { 5, 3, 5, 3, 2 };
private Singleton()
{
// private.
}
public static void main(String... args) throws InterruptedException
{
A a = new C(5);
a.print();
A clonedObj = a.Clone();
clonedObj.print();
}