Something Similar can be achieved in newer Java versions with sealed types:
sealed interface MyResult permits MyResult.Alpha, MyResult.Beta {
record Alpha(int total) implements MyResult {}
record Beta(String name) implements
MyResult {}
}
You can further match using enhanced switch statements:
void consumeResult(MyResult result) {
switch (result) {
case MyResult.Alpha alpha: System.out.println("Total: "+alpha.total()); break;
case MyResult.Beta beta: System.out.println("Name: "+beta.name()); break;
}
}
This is sadly only available in higher versions, but similar inheritance structures can be used to implement ADTs in lower versions. If you can't create sealed classes, you will need to use an if else branching system, as well as a (package) private constructor to restrict instantiations through other means.
Is this something along the lines of what would be useful to you?