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 ?
  • It's basicly dependency injection made mandatory. What actually will be called when a method is called can't be known at compile time since now it can be changed at run time. In languages that don't support this we work around it when needed. – candied_orange Aug 2 '16 at 4:08
  • 1
    @Telastyn that wiki article is about class-based programming not runtime inheritance. The languages listed at the end are just OOP languages. – candied_orange Aug 2 '16 at 4:11
  • They stuck that term into the article in the summary, but never mention it again throughout the remainder of the article. "Runtime Inheritance" isn't even a Googlable term; I suggest you read the rest of the article and forget that term is even there. – Robert Harvey Aug 2 '16 at 5:06
  • 1
    Both RobertHarvey & DocBrown make good points. As does the OP: the text with that phrasing is a rather unclear if not somewhat misleading , and hence I see this question is reasonable. I think what the text is getting at is that the runtime knows the actual and true class (whereas with untagged, interchangeable struct's that is not always the case). – Erik Eidt Aug 2 '16 at 6:10
  • @DocBrown Sorry for posting my question with less clarity and basing it on the Wikipedia article, as it was not the only source of my doubt (which led to this question). I have edited my question. Please do see it. :) – kaartic Aug 2 '16 at 12:30
up vote 5 down vote accepted

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
  1. Pretty sure it's called Prototypal Inheritance:

    Many prototype-based systems encourage the alteration of prototypes during run-time, whereas only very few class-based object-oriented systems (...) allow classes to be altered during the execution of a program.

  2. Usually dynamic (scripting) languages such as JavaScript and Lua.

  3. When it's native to a programming language or in a dynamic programming scenario.

  • 3
    Sorry, but prototypal inheritance is just one way of having dynamic inheritance. You even quote an article which talks about class-based OO systems (CLOS, Smalltalk). Besides, dynamic != scripting – coredump Aug 2 '16 at 15:31

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.