I had a question on an interview where I had 2 types of foods, say bread and cake, was given the following:
public class FoodFactory{};
public class Food{};
public static void main(String[] args) {
Food foo = FoodFactory.get("bread");
System.out.println(foo.getClass().getName());
}
Given the FoodFactory class and Food class, I wasn't sure how to make this work so that it would print out "bread" for the class. It really seemed like the only way to have gotten it to print bread was to create a Bread class and have the FoodFactory return that.
I'm just wondering if I'm missing anything, because FoodFactory and Food were classes given so I assumed those were the only 2 classes I needed to change. Is there a way to have implemented the two classes given and made it print "bread" while using just those 2 classes?
FoodI suppose. So the factory choose instance of which class to produce taking argument into account.getClass().getName()is never going to print "Bread".