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 (object-oriented programming), inheritance occurs via defining ''class (computer programming), classes'' of object ( ...
, 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 or ...
a ''base'' or ''parent'' class reference, to a more restricted '' derived class'' reference. This is only allowable if the object is already an instance of the derived class, and so this conversion is inherently fallible. In contrast ''upcasting'', explicitly treating an object as if it's an instance of one of its superclasses, is always possible. In many environments,
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 ...
can be used to obtain the type of an object instance at runtime, and then use this result to explicitly evaluate its
type compatibility Type may refer to: Science and technology Computing * Typing, producing text via a keyboard, typewriter, etc. * Data type, collection of values used for computations. * File type * TYPE (DOS command), a command to display contents of a file. * ...
with another type. The possible results of comparing polymorphic types—besides them being equivalent (identical), or unrelated (incompatible)—include two additional cases: namely, where the first type is derived from the second, and then the same thing but swapped the other way around (see: ). With this information, a program can test, before performing an operation such as storing an object into a typed variable, whether that operation is type safe, or whether it would result in an error. If the type of the runtime instance is derived from (a child of) the type of the target variable (therefore, the parent), downcasting is possible. Some languages, such as
OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
, 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. Compile-time downcasting is implemented by static_cast, 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 is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
containers before Java generics were launched, which requires downcasting of the contained objects so that they can be utilised 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