Lets say I have Object X and Object B. X is an object of a random class, which in turn creates B, and passes itself on to be stored as a variable in object B.
Now object B, wants to run a method in object X, without knowing what class it is.
Is it possible to call a method in any object, and if a method with that name exist, it runs, and if not, it doesn't. I assume some try/catch can work around that part.
But lets say i have something like this:
public class ObjectB {
public Object parentX;
public ObjectB(Object x) {
parentX = x;
}
public void anyMethod() {
x.runMyMethod();
}
}
In another program I worked with, this was possible with interfaces, and therefore have an idea that it is possible with Java too. However, i cant seem to find the way to set this up. So if the above was the case, how would it be set up, such that ObjectB can call the method on all the classes in my program if that was what i wanted?