Runtime Inheritance – Understanding Inheritance in Programming Languages

inheritanceprogramming-languages

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 :

  1. What exactly is runtime inheritance ?
  2. What type of languages support it ?
  3. 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.

class NewClass {
    //Some methods and stuff
}
class ChildClass extends NewClass {
   //Some methods and stuff
}

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:

class NewClass():
    pass

class ChildClass(NewClass):
    pass

Or in the interpreter on the fly:

>>> NewClass()
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    NewClass()
NameError: name 'NewClass' is not defined
>>> class NewClass():
    pass

>>> NewClass()
<__main__.NewClass object at 0x02AC6970>
>>> ChildClass()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    ChildClass()
NameError: name 'ChildClass' is not defined
>>> class ChildClass(NewClass):
    pass

>>> nc = NewClass()
>>> cc = ChildClass()
>>> nc
<__main__.NewClass object at 0x02E9B190>
>>> cc
<__main__.ChildClass object at 0x02E9B150>

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).

>>> NewClass
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    NewClass
NameError: name 'NewClass' is not defined
>>> NewClass = type("NewClass", (), {})
>>> NewClass()
<__main__.NewClass object at 0x02AA6970>
>>> ChildClass
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    ChildClass
NameError: name 'ChildClass' is not defined
>>> ChildClass = type("ChildClass", (NewClass,), {})
>>> ChildClass()
<__main__.ChildClass object at 0x02EBB190>
>>> 

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:

  • Implementing an interpreter where the programmer can define types
  • Creating appropriate data types when reading in data, such as a network remote procedure call, or creating DOM node types based on an XML schema
  • Any kind of simulation software where the user can create their own category of objects and then make some of them.
  • Creating a new class to represent typeclasses in a regular expression parser