Suppose I have two custom classes and a method as follows:
class A {
public void think() {
// do stuff
}
}
class B {
public void think() {
// do other stuff
}
}
Class C {
public void processStuff(A thinker) {
thinker.think();
}
}
Is there a way to write processStuff()as anything like this (just illustrating):
public void processStuff({A || B} thinker) {...}
Or, in other words, is there a way to write a method with a one parameter that accepts multiple types, as to avoid typing the processStuff() method multiple times?
Define the behavior you want in an interface, have
AandBimplement the interface, and declare yourprocessStuffto take as an argument an instance of the interface.