0

I have the following classes in Java

class A which is inherited by class A1 and A2

A has method run and both A1, A2 are overriding this method

Another model class B

B has JSON subtypes B1 and B2 (child classes)

What I want to achieve now is if I run

InstanceOfA.run(B) where B is a reference to B1 subtype, it should execute A1's run method

and If I run InstanceOfA.run(B) where B is a reference to B2 subtype, It should execute A2's run method.

InstanceOfA is an instance of A.

If this is possible, I can inject A using @Inject annotation in all other classes (where it is required) and don't need to pass actual implementations.

Any other solution to do is also welcome.

LiorH
  • 18,524
  • 17
  • 70
  • 98
Dinesh Chander
  • 117
  • 2
  • 12
  • Basic concept of inheritance is that parent does not know child. Parent can be interface, abstract or fully implemented class. With that ```class A``` should never know existence of A1 and A2. – Jags Mar 10 '20 at 16:26
  • 1
    It looks like you have [parallel inheritance hierarchies](https://stackoverflow.com/questions/696350/avoiding-parallel-inheritance-hierarchies). – jaco0646 Mar 10 '20 at 19:16

0 Answers0