Downcasting
   HOME

TheInfoList



OR:

In
class-based programming Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance occurs via defining ''classes'' of objects, instead of inheritance occurring via the objects alone (compare prototy ...
, downcasting or type refinement is the act of
casting Casting is a manufacturing process in which a liquid material is usually poured into a mold, which contains a hollow cavity of the desired shape, and then allowed to solidify. The solidified part is also known as a ''casting'', which is ejected ...
a reference of a base class to one of its derived classes. In many
programming languages A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
, it is possible to check through
type introspection In computing, type introspection is the ability of a program to ''examine'' the type or properties of an object at runtime. Some programming languages possess this capability. Introspection should not be confused with reflection, which goes a st ...
to determine 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. In other words, when a variable of the base class (
parent class In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
) has a value of the derived class ( child class), downcasting is possible. Some languages, such as
OCaml OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
, disallow downcasting.


Examples


Java

public class Fruit // parent class public class Apple extends Fruit // child class public static void main(String args[])


C++

// Parent class: class Fruit ; // Child class: class Apple : public Fruit ; int main(int argc, const char** argv)


Uses

Downcasting is useful when the type of the value referenced by the Parent variable is known and often is used when passing a value as a parameter. In the below example, the method objectToString takes an Object parameter which is assumed to be of type String. public static String objectToString(Object myObject) public static void main(String args[]) In this approach, downcasting prevents the compiler from detecting a possible error and instead causes a run-time error. Downcasting myObject to String ('(String)myObject') was not possible at compile time because there are times that myObject is String type, so only at run time can we figure out whether the parameter passed in is logical. While we could also convert myObject to a compile-time String using the universal java.lang.Object.toString(), this would risk calling the default implementation of toString() where it was unhelpful or insecure, and exception handling could not prevent this. In C++, run-time type checking is implemented through
dynamic_cast In computer programming, run-time type information or run-time type identification (RTTI) is a feature of some programming languages (such as C++, Object Pascal, and Ada) that exposes information about an object's data type at runtime. Run-time ty ...
. Compile-time downcasting is implemented by
static_cast In the C++ programming language, static_cast is an operator that performs an explicit type conversion. Syntax static_cast (object); The ''type'' parameter must be a data type to which ''object'' can be converted via a known method, whether ...
, but this operation performs no type check. If it is used improperly, it could produce undefined behavior.


Considerations

A popular example of a badly considered design is containers of
top type In mathematical logic and computer science, some type theories and type systems include a top type that is commonly denoted with top or the symbol ⊤. The top type is sometimes called also ''universal type'', or ''universal supertype'' as all oth ...
s, like the
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
containers before Java generics were introduced, which requires downcasting of the contained objects so that they can be used again.


See also

* Subtype polymorphism


References

{{Reflist


External links


Downcasting is a Code Smell
by Jeremy D. Miller
A downcasting tragedy
by Jimmy Bogard

by Bill Venners
Downcasting in C#
by Scott Lysle
Multiple downcasting techniques
by Sinipull Class (computer programming) Articles with example Java code