I recently came across the term "runtime inheritance" when reading this Wikipedia article. I tried to search for it and came across this article that tries to explain the difference between runtime and compile time inheritance, in which they explain runtime inheritance as the "ability to construct the parent/child hierarchy tree at runtime ". It also explains that it could be achieved in Java by using tools that modify the bytecode after compilation of the source, as Java does not natively support it. It did not give a clear idea about how it could be used or what languages support it natively.
I would like to know more about runtime inheritance. I would like answers for these :
- What exactly is runtime inheritance ?
- What type of languages support it ?
- Any situation in which it could prove useful ?
Best Answer
Runtime Inheritance
Runtime inheritance is, as you say, constructing the parent/child hierarchy tree at runtime. However, you seem unsure as to what that might mean or look like. In languages like Java, you basically have to define the class hierarchy at compile time. That means your code has to have this general structure.
It's not really possible (read: convenient), outside of very arcane tricks like modifying byte code on the fly, to define a new class. Contrast that with Python, where similar code can be done like this:
Or in the interpreter on the fly:
Here, I've created classes of the same names as in Java, but I've done it on the fly in an interpreter. No compile time restrictions at all. But wait, there's more! You can create a new class, on the fly, with just three things: a name, an inheritance order tuple, and a namespace dictionary (or dictionary-like object).
Note how in this language you can create a new class, possibly using variables for the name, inheritance order, and namespace. Without even using the class keyword. That is runtime inheritance.
As a closing note for this section, I don't want to give the impression that this is somehow the only way to do it. Prototype based inheritance as implemented in JavaScript is quite a different approach to this, and supports fascinating and useful (but also very dangerous) things like reassigning the parent class on an existing object.
What type of languages support it
I don't think there's really a good way to classify it without being circular and saying "Runtime Inheritance is supported in Runtime Inheritable languages". Depending on who you talk to, I'm confident you can find this type of feature in any paradigm (functional, procedural, object-oriented, etc.). It's really just another language feature, albeit a powerful one.
Any situation in which it could prove useful ?
Absolutely. Options include, but aren't limited to, these: