Inheritance during run time

there is a way to inherit the parent who create the child.
in the diagram below you can see that i need to implement 6 different cases, Im sure it’s not the best practice. if there is any other solutions I would be glad to hear .

the case is somtime class 1-3 need to inherit from class c and some time from class d

It seems that this arise from the need of sharing codes. If that’s the case, then please elaborate your concreate use-case. Especially, why you need to do this?

Generally speaking, code sharing via inheritance brings unwarranted complexity. That’s why, code reuse via composition is generally a better option than inheritance.

Hi, this is not an Aurelia problem but a general inheritance problem. Some languages allow multiple inheritance (e.g. C++). Javascript doesn’t support it, but you can “workaround” with mixin patterns. Typescript have it as part of the language definition:

With plain javascript you can achieve with with some extra effort:

I personally never found a real case where multiple inheritance justifies the complexity it generates. You can solve it with other patterns. As @Sayan751 mentioned, knowing your concrete use-case will help us to give a better answer.

1 Like

thanks alot @Sayan751 @bmarotta
the case is that we have 2 ways for payment - one is in regular order and one is just a link where the logic is much simple.
so we have class payment that have the generic staff, and two classes inherit from it for order payment and link payment,
BUT
we have different payment 3rds party, each one need their fields and logic , the base logic should inherit from order payment / link payment …
the only way I know is to make classes for each of the payment case .
we should handle it using JS :\

You need dependency injection. Considering payment and link payment are 2 different set of behaviors that implement a single contract/interface, then you can inject these behaviors in you payment classes and you write your code simply against the interface.

thaks !! can you please tell me if I understand it correctly ?
u mean that order payment and link payment will inherit from base class payment , the, using DI I will inject the relevant behavior, to the 3rd party component instead of inherit it ?

That’s correct! That way the individual payment classes “has” that behavior instead of “being” that behavior. And that same way those classes can “have” many more other behaviors, w/o necessarily being all those behaviors.

1 Like