Downcasting
Encyclopedia
In object-oriented programming
, downcasting or type refinement is the act of casting
a reference of a base class to one of its derived classes.
In most programming languages, it is usually possible to check through RTTI whether the type of the referenced object is indeed the one being cast to or a derived type of it, and thus issue an error if it is not the case.
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, downcasting or type refinement is the act of casting
Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...
a reference of a base class to one of its derived classes.
In most programming languages, it is usually possible to check through RTTI whether the type of the referenced object is indeed the one being cast to or a derived type of it, and thus issue an error if it is not the case.
External links
- Article "Downcasting is a Code Smell" by Jeremy D. Miller
- Article "A downcasting tragedy" by Jimmy Bogard
- Article "Prefer polymorphism over instanceof and downcasting" by Bill Venners
- Article "Downcasting in C#" by Scott Lysle
- Paper "Multiple downcasting techniques"