1

I have method with super class as argument in main class.

public class MainClass() {
...
    public void myMethod(MyClass class)
...

In runtime this object can be any of its subclasses and it should call different method depending on subclasses in same main class

public class MainClass() {
...
    public void mySubMethod(MySubClass1 class)

    public void mySubMethod(MuySubClass2 class)
...

Of course, I can use instanceOf or Static Factory Pattern with Enum but still it will be (pseudo code)

public void myMethod(MyClass class) {
if (instanceOf == MySubclass1) mySubMethod((MySubClass1)subClass1Object)
if (instanceOf == MySubclass2) mySubMethod((MySubClass2)subClass2Object)
}

In runtime jvm knows which subclass exactly object is and knows for each sub-method argument should be. I don't have control over MyClass, MySubclass1 and MySubclass2,...

But is there any elegant way to achive this?

user1182625
  • 145
  • 2
  • 15
  • Why not simply overload `myMethod()`? – ernest_k Jun 01 '20 at 10:37
  • @ernest_k overloading only works on static types, it is not dispatched dynamically. – Thilo Jun 01 '20 at 10:40
  • Related: https://stackoverflow.com/questions/4737350/polymorphically-dispatching-in-java – Thilo Jun 01 '20 at 10:43
  • @Thilo The OP doesn't mention that the object is created dynamically. It's possible that this "problem" is a result of how `myMethod` is currently declared. Maybe it can change. – ernest_k Jun 01 '20 at 10:49
  • Object are created dynamically. It depends what I get from other method. – user1182625 Jun 01 '20 at 15:01
  • 3
    You code would be much more readable if you didn’t use the keyword `class` as (invalid) parameter name throughout your code. – Holger Jun 02 '20 at 08:03
  • Sorry, it was pseudo-code. I send object. I get object in generic way, as super class upcast object of one of its sub class. In fact it is abstract class. And then I should call one of two methods. One method accept one subclass as variable, and other method the other subclass as variable. JVM knows which exactly subclass upcasted object is. I just want to do it without switch and anything similar to instanceof, if there is more elegant way. – user1182625 Jun 02 '20 at 09:27

2 Answers2

1

You could use "double-dispatch" by moving those methods to the various target subclasses themselves.

interface MyClass {
   void mySubMethod(Something context);
}

public void myMethod(MyClass target){
   target.mySubMethod(this);
}
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I updated situation. Point is that I don't have control over MyClass and MySubclassN. This would be good solution. – user1182625 Jun 01 '20 at 20:01
1

One solution to this is to use HandlerFactories

public class HandlerRegistry {
  public void registerHandler(Class classToHandle, HandlerFactory handler)
  // returns the proper handler (via the HandlerFactory) for the registered class
  public Handler getHandlerFor(Class classToHandle)
}

You can make the methods in this class static or use the Singleton pattern to make the HandlerRgistry accessible everywhere }

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80