Concept

Downcasting

In class-based programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes. In many programming languages, it is possible to check through type introspection 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) has a value of the derived class (child class), downcasting is possible. Some languages, such as OCaml, disallow downcasting. public class Fruit{} // parent class public class Apple extends Fruit{} // child class public static void main(String[] args) { // The following is an implicit upcast: Fruit parent = new Apple(); // The following is a downcast. Here, it works since the variable parent is // holding an instance of Apple: Apple child = (Apple)parent; //Where Apple is Child Class But Fruit is Parent Class } // Parent class: class Fruit { public: // Must be polymorphic to use runtime-checked dynamic-cast. virtual ~Fruit() = default; }; // Child class: class Apple : public Fruit {}; int main(int argc, const char** argv) { // The following is an implicit upcast: Fruit* parent = new Apple(); // The following is a downcast. Here, it works since the variable parent is // holding an instance of Apple: Apple* child = dynamic_cast(parent); } 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) { // This will only work when the myObject currently holding value is string. return (String)myObject; } public static void main(String[] args) { // This will work since we passed in String, so myObject has value of String. String result = objectToString("My String"); Object iFail = new Object(); // This will fail since we passed in Object which does not have value of String.

À propos de ce résultat
Cette page est générée automatiquement et peut contenir des informations qui ne sont pas correctes, complètes, à jour ou pertinentes par rapport à votre recherche. Il en va de même pour toutes les autres pages de ce site. Veillez à vérifier les informations auprès des sources officielles de l'EPFL.

Graph Chatbot

Chattez avec Graph Search

Posez n’importe quelle question sur les cours, conférences, exercices, recherches, actualités, etc. de l’EPFL ou essayez les exemples de questions ci-dessous.

AVERTISSEMENT : Le chatbot Graph n'est pas programmé pour fournir des réponses explicites ou catégoriques à vos questions. Il transforme plutôt vos questions en demandes API qui sont distribuées aux différents services informatiques officiellement administrés par l'EPFL. Son but est uniquement de collecter et de recommander des références pertinentes à des contenus que vous pouvez explorer pour vous aider à répondre à vos questions.