I have two classes, class A and class B, which each have a method with the same name. Is it possible to have one variable, maybe of type Object
, that could be able to contain both types of classes and perform their methods?
As an example of what I mean:
class A {
public void doSomething() {
...
}
}
class B {
public void doSomething() {
...
}
}
class Test {
Object obj;
if (isTrue)
obj = new A();
else
obj = new B();
obj.doSomething();
}
I looked into Reflection, but I am not sure if it can be used to do what I want or not.
Any help is cool, thanks.