# Transition Inheritance

A core objective of UML2 is to reduce duplication of code.

As such UML2 supports the concept of 'inherited' transitions.

When you define a transition in a parent state each of the child states 'inherit' that transition.

```
 var machine = StateMachine.create((g) => g
    ..initialState<S>()
    ..state<Alive>((b) => b
      ..on<OnDeath, Dead>()
      ..state<Young>((b) => b)
      ..state<Old>((b) => b))
    ..state<Dead>((b) => b)
```

In the above example we have four states:

* Alive
* Young
* Old
* Dead

It doesn't matter if you are Young or Old you can always die.

FMS2 allows us to place the '..on\<OnDeath, Dead>' transition in the parent 'Alive' state. We now say that both the Young and Old states inherit the OnDeath transition. This correctly models the fact that you can die at any moment (so stop programming right now and go out and enjoy the day!).

## Handling of inherited transitions

So what occurs when stateMachine.applyEvent(OnDeath()) is called?

When using nested states or concurrent regions you state machine can be in multiple states simultaneously.

In the above example if you are Young then you are also Alive, so we can say that you are in both the 'Alive' and the 'Young' state.

Our [StateOfMind](/tracking-state/stateofmind.md) object would have a single StatePath 'Alive -> Young'. If you had active [concurrent regions](/states/concurrent-states.md) then the StateOfMind would have a StatePath for each active concurrent state.

When searching for a transition handler for the given event we start at leaf state (Young) and search up the tree until we find a matching handler. In this case we find one on the 'Alive' state.&#x20;

A single transition event will be emitted to all transition listeners which will indicate that an event was handled by the 'Young' state even though the transition handler was in the 'Alive' state.

A single event can result in multiple transition events if you are in a concurrent region or an 'onFork' transition is triggered.

A single StateOfMind will be added to the stream  (StateMachine.stream) regardless of the number of transitions that occur when processing the event.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fsm2.onepub.dev/transitions/transition-inheritance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
