Subclassing Body (patch design query) (dup from JBox2D)

(Duplicated from my post in JBox2D forum but realised (a) my query applies more generally and (b) this forum's read more? Please delete other if needed 🙂 )

Hi,

TL;DR How would one create a Body subclass instead of a Body?

In both/either original (C++) and/or JBox2D (Java).
I'm working on a patch for DartBox2D (the Dart port) and want to keep it as similar as possible to any existing solution.

In case I'm approaching the wrong way, I'll explain my need:
I wish to have some logic run when a Body is put to sleep (setAwake(false)) -- specifically, I switch it to a STATIC Body since it won't move again in my game (and this seems efficient).

My approach:
At least in the Java and Dart versions (the Dart version is a direct port of the Java) and AFAICT in the C++ version, there is no event dispatched (etc) here so my solution is subclassing Body (with extra logic in setAwake()) then modifying Box2D (my patch) to accept a BodyFactory on World (which, when not supplied, defaults to one that simply builds a Body as the current code). Since I my game builds both SleepyBody and regular ones, I include a switch in my implementation of BodyFactory to choose which to make before my call to createBody() which I invoke through a reference to my SleepyBodyFactory. (yes, I'm aware of parallelism risks -- not a problem in DartBox2D.)

All questions or redirects welcome (perhaps I should post outside the JBox2D sub-forum?)

If not presently viable but considered useful, should I try for patches to other versions?

TIA, Rupert.
I don't know about the Java port, but in the C++ version a b2Body is very much not intended to be subclassed.
Would it be possible to put the modification in the SetAwake function itself?
Thanks for the prompt reply, irresistible (ok, *that* just sounds weird!)

Happy to hear alternative approaches. How would you do that?
My C++ is not as good as my Java/Dart so I'm probably missing something obvious <doh>. The only way I know to add functionality to a class' method (unless one wants to faff with code generation!) is to subclass and override (what my patch does). That's true for Dart (unless I add function pointers, leading us down a messier rabbit hole) and Java.

In a different direction, when you say "not intended too be subclassed", could I ask why? (performance... ?) (always keen to learn more 🙂 )

Thanks again!
P.s. for the curious, here's the code review with my change -- join in there or in the issue if you prefer 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=8861