What is the appropriate way to initialize an implemented class of an interface (determined by some logic)
Example
IAnaimal is an interface
Cat -> IAnimal
Dog -> IAnimal
Cow -> IAnimal
int x = in.nextInt();
IAnimal animal = null;
if(x==1)
animal = new Dog();
else if(x==2)
animal = new Cat();
else
animal = new Cow();
animal.destroyManKind();
Is this the correct approach? Is there a 'more' professional way of doing this?
in.next()came from? You might use aswitch instead ofif-else of -else`classor direct-type.Dog d = new Dog();