First of all, BalusC is right.
Secondly:
If you make decisions based on the type of class, you do not allow polymorphism to do its job.
Your class structure may be incorrect (for example, Car and Person should not be in the same hierarchy)
Perhaps you can create an interface and code for it.
interface Fooable { Fooable createInstance(); void doFoo(); void doBar(); } class Car implements Fooable { public Fooable createInstance() { return new Car(); } public void doFoo(){ out.println("Brroooom, brooooom"); } public void doBar() { out.println("Schreeeeeeeekkkkkt"); } } class Person implements Fooable { public Fooable createInstance(){ return new Person(); } public void foo() { out.println("ehem, good morning sir"); } public void bar() { out.println("Among the nations as among the individuals, the respect for the other rights means peace..");
Later...
public class Xpto{ protected Fooable x; public void foo(){ Fooable y = x.createInstance();
source share