Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

As far as I know, the terminology comes from how the inheritance hierarchy is traditionally displayed, with the extending types at the bottom, and the parent types at the top.

This is a bit pointless, unless you feel like calling the following leftcasting and rightcasting.

horizontal inheritance hierarchy

I am not looking for opinions why the terminology is as is, but they are more than welcome as comments. I am looking for references on who first introduced up and downcasting, and why they decided on that name.

share|improve this question
1  
Although they're somewhat self-explanatory, I haven't heard anyone actually use these terms. Have ye a reference? IOW: "You upcast to an object which can do less, and you downcast to an object which can do more." [citation needed] –  Task Dec 22 '11 at 14:41
1  
I consider upcasting safer (you go towards a type that's guaranteed to have at least a subset of the members of the starting type), whereas when downcasting you can't be really sure of what you'll find. Safer -> better, so the typical reasoning you use, works. [/opinion] –  Yannis Rizos Dec 22 '11 at 14:43
2  
And quite more importantly. Did you read the article from where you got the dog / cat image? If not, it supports my previous comment: upcasting is safer, as it can never fail, downcasting on the other hand can. Also I'd add that upcasting gets us at a higher abstraction level (if that makes it easier for you to remember). –  Yannis Rizos Dec 22 '11 at 15:24
1  
It's not so easy to search for their origins because upcasting and downcasting have older meanings. But perhaps those meanings influenced the OOP words. But the Ngram shows big increases around 1980 and 1995. The earliest certain references to downcast[ing] I found are from 1992 in C++ literature... –  Hugo Dec 23 '11 at 11:32
2  
@StevenJeuris I'm not seeing any reopen votes on it now (they might've aged away), but I'll give it a shot. Reopened. –  Anna Lear Jan 13 '12 at 16:12

2 Answers 2

Basically the down- and up-cast is the logical continuation of the subtype (subclass in OOP) and supertype (superclass respectively), e.g. the classical representation of the derived type to be located below the original type -- subtype.

If we search for the term subtyping we find the following (Wikipedia):

The notion of subtyping in programming languages dates back to the 1960s; it was introduced in Simula derivatives. The first formal treatments of subtyping were given by John C. Reynolds in 1980 who used category theory to formalize implicit conversions, and Luca Cardelli (1985).

So, as many other things, it originated in something vaguely called here as "Simula derivatives" (probably early OOP languages). Simula, however, already has the notion of subclass, but does not feature all the properties of OOP.

share|improve this answer
    
As stated in my question: "the terminology comes from how the inheritance hierarchy is traditionally displayed". This doesn't explain the reason why "sub" and "super" should be displayed as "up" and "down". I already added SIMULA as a comment. –  Steven Jeuris Dec 22 '11 at 15:42
1  
Hmm... So, you don't see connection between "sub" and "down"? What about subtitles, Subzero, subliminal and other common English words? –  Alexander Galkin Dec 22 '11 at 15:50
1  
P.s.: In SIMULA the concept of inheritance was originally called, "concatenation", and later "prefixing". I don't recall reading about subtyping or supertyping when reading the SIMULA documentation. I believe this is a term which only emerged later. –  Steven Jeuris Dec 22 '11 at 15:51
    
Just did some rereading of the SIMULA documentation. Subclass is indeed used, but not subtype. By rereading your answer I now know what you meant. :) Would still prefer an actual reference, but I see how you go from "sub" to lower. –  Steven Jeuris Dec 23 '11 at 11:51
    
Section 3.2.5: "An object reference is said to be "subordinate" to a second object reference if the qualification of the former is a subclass of the class which qualified the latter. A proper procedure is said to be of "type universal". Any type is subordinate to the universal type." –  Steven Jeuris Dec 23 '11 at 11:53

The oldest reference I've found yet is from Sep 1990, in a Usenet post.

The library referenced there is the NIHCL (available from the Software Preservation Group), which contains this code (MI is "multiple inheritance"):

#ifdef MI

#define DECLARE_CASTDOWN(classname) \
    static classname& castdown(Object& p) \
        { return *(classname*)(&p ? p._safe_castdown(*desc()) : 0); } \
    static const classname& castdown(const Object& p) \
        { return *(const classname*)(&p ? p._safe_castdown(*desc()) : 0); } \
    static classname* castdown(Object* p) \
        { return (classname*)(p ? p->_safe_castdown(*desc()) : 0); } \
    static const classname* castdown(const Object* p) \
        { return (const classname*)(p ? p->_safe_castdown(*desc()) : 0); } \

#else

#define DECLARE_CASTDOWN(classname) \
    static classname& castdown(Object& p)           { return (classname&)p; } \
    static const classname& castdown(const Object& p)   { return (const classname&)p; } \
    static classname* castdown(Object* p)           { return (classname*)p; } \
    static const classname* castdown(const Object* p)   { return (const classname*)p; } \

#endif

The book that this code was included with (Data Abstraction and Object-Oriented Programming in C++) also uses the term "castdown".

The term "castdown" also seems to predate "downcast", at least on Usenet.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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