47:
629:
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.
628:
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
152:
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
476:
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.
144:
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:
137:
781:
129:
reference. This is only allowable if the object is already an instance of the derived class, and so this conversion is inherently fallible.
68:
35:
90:
736:
141:
742:
690:
754:
653:
136:
can be used to obtain the type of an object instance at runtime, and then use this result to explicitly evaluate its
61:
55:
72:
765:
715:
640:, but this operation performs no type check. If it is used improperly, it could produce undefined behavior.
104:
669:
660:
were introduced, which requires downcasting of the contained objects so that they can be used again.
133:
112:
748:
31:
775:
657:
633:
637:
602:// This will fail since we passed in Object which does not have value of String.
560:// This will work since we passed in String, so myObject has value of String.
427:// The following is a downcast. Here, it works since the variable `parent` is
259:// The following is a downcast. Here, it works since the variable `parent` is
146:
125:
760:
509:// This will only work when the myObject currently holding value is string.
649:
714:
Vouillon, Jérôme; Rémy, Didier; Garrigue, Jacques (September 12, 2013).
27:
Act of casting a reference of a base class to one of its derived classes
17:
720:
The OCaml system release 4.01 : Documentation and user's manual
157:
648:
A popular example of a badly considered design is containers of
40:
319:// Must be polymorphic to use runtime-checked dynamic-cast.
632:
In C++, run-time type checking is implemented through
140:
with another type. The possible results of comparing
749:
Prefer polymorphism over instanceof and downcasting
153:(therefore, the parent), downcasting is possible.
636:. Compile-time downcasting is implemented by
8:
30:"Downcast" redirects here. For the app, see
91:Learn how and when to remove this message
54:This article includes a list of general
681:
403:// The following is an implicit upcast:
238:// The following is an implicit upcast:
123:class reference, to a more restricted
7:
111:, or type refinement, is the act of
36:Glossary of coal mining terminology
691:"How to: Use safe_cast in C++/CLI"
60:it lacks sufficient corresponding
25:
430:// holding an instance of Apple:
262:// holding an instance of Apple:
45:
34:. For the coal mining term, see
761:Multiple downcasting techniques
1:
782:Class (computer programming)
147:Subtyping § Subsumption
737:Downcasting is a Code Smell
798:
29:
689:TylerMSFT (2021-08-03).
479:
298:
172:
160:, disallow downcasting.
156:Some languages, such as
105:class-based programming
75:more precise citations.
766:Upcasting, downcasting
132:In many environments,
743:A downcasting tragedy
578:"My String"
670:Subtype polymorphism
739:by Jeremy D. Miller
695:learn.microsoft.com
716:"Objects in OCaml"
656:containers before
138:type compatibility
134:type introspection
755:Downcasting in C#
101:
100:
93:
16:(Redirected from
789:
724:
723:
711:
705:
704:
702:
701:
686:
624:
621:
618:
615:
612:
609:
606:
603:
600:
597:
594:
591:
588:
585:
582:
579:
576:
573:
570:
567:
564:
561:
558:
555:
552:
549:
546:
543:
540:
537:
534:
531:
528:
525:
522:
519:
516:
513:
510:
507:
504:
501:
498:
495:
492:
489:
486:
483:
467:
464:
461:
458:
455:
452:
449:
446:
443:
440:
437:
434:
431:
428:
425:
422:
419:
416:
413:
410:
407:
404:
401:
398:
395:
392:
389:
386:
383:
380:
377:
374:
371:
368:
365:
362:
359:
356:
353:
350:
347:
344:
341:
338:
335:
332:
329:
326:
323:
320:
317:
314:
311:
308:
305:
302:
301:// Parent class:
290:
287:
284:
281:
278:
275:
272:
269:
266:
263:
260:
257:
254:
251:
248:
245:
242:
239:
236:
233:
230:
227:
224:
221:
218:
215:
212:
209:
206:
203:
200:
197:
194:
191:
188:
185:
182:
179:
176:
96:
89:
85:
82:
76:
71:this article by
62:inline citations
49:
48:
41:
21:
797:
796:
792:
791:
790:
788:
787:
786:
772:
771:
751:by Bill Venners
745:by Jimmy Bogard
733:
728:
727:
713:
712:
708:
699:
697:
688:
687:
683:
678:
666:
646:
626:
625:
622:
619:
616:
613:
610:
607:
604:
601:
598:
595:
592:
589:
586:
583:
580:
577:
574:
571:
568:
565:
562:
559:
556:
553:
550:
547:
544:
541:
538:
535:
532:
529:
526:
523:
520:
517:
514:
511:
508:
505:
502:
499:
496:
493:
490:
487:
484:
481:
474:
469:
468:
465:
462:
459:
456:
453:
450:
447:
444:
441:
438:
435:
432:
429:
426:
423:
420:
417:
414:
411:
408:
405:
402:
399:
396:
393:
390:
387:
384:
381:
378:
375:
372:
369:
366:
363:
360:
357:
354:
351:
348:
346:// Child class:
345:
342:
339:
336:
333:
330:
327:
324:
321:
318:
315:
312:
309:
306:
303:
300:
297:
292:
291:
288:
285:
282:
279:
276:
273:
270:
267:
264:
261:
258:
255:
252:
249:
246:
243:
240:
237:
234:
231:
228:
225:
222:
219:
216:
213:
210:
207:
204:
201:
198:
195:
192:
189:
187:// parent class
186:
183:
180:
177:
174:
171:
166:
97:
86:
80:
77:
67:Please help to
66:
50:
46:
39:
28:
23:
22:
15:
12:
11:
5:
795:
793:
785:
784:
774:
773:
770:
769:
763:
758:
757:by Scott Lysle
752:
746:
740:
732:
731:External links
729:
726:
725:
706:
680:
679:
677:
674:
673:
672:
665:
662:
645:
644:Considerations
642:
611:objectToString
572:objectToString
491:objectToString
480:
473:
470:
299:
296:
293:
208:// child class
173:
170:
167:
165:
162:
99:
98:
53:
51:
44:
32:Downcast (app)
26:
24:
14:
13:
10:
9:
6:
4:
3:
2:
794:
783:
780:
779:
777:
767:
764:
762:
759:
756:
753:
750:
747:
744:
741:
738:
735:
734:
730:
721:
717:
710:
707:
696:
692:
685:
682:
675:
671:
668:
667:
663:
661:
659:
658:Java generics
655:
651:
643:
641:
639:
635:
630:
478:
471:
294:
168:
163:
161:
159:
154:
150:
148:
143:
139:
135:
130:
128:
127:
126:derived class
122:
118:
114:
110:
106:
95:
92:
84:
74:
70:
64:
63:
57:
52:
43:
42:
37:
33:
19:
719:
709:
698:. Retrieved
694:
684:
647:
634:dynamic_cast
631:
627:
475:
445:dynamic_cast
155:
151:
131:
124:
120:
116:
108:
102:
87:
78:
59:
768:by Sinipull
652:, like the
638:static_cast
142:polymorphic
109:downcasting
81:August 2008
73:introducing
700:2023-12-01
676:References
56:references
650:top types
776:Category
664:See also
524:myObject
500:myObject
164:Examples
18:Downcast
337:default
322:virtual
199:extends
113:casting
69:improve
605:result
596:Object
584:Object
566:result
563:String
548:String
536:static
533:public
518:String
512:return
497:Object
488:String
485:static
482:public
460:parent
412:parent
358:public
313:public
283:parent
244:parent
226:String
214:static
211:public
190:public
175:public
121:parent
58:, but
617:iFail
587:iFail
454:*>
451:Apple
439:child
433:Apple
421:Apple
406:Fruit
385:const
361:Fruit
352:Apple
349:class
328:Fruit
307:Fruit
304:class
277:Apple
268:child
265:Apple
253:Apple
241:Fruit
202:Fruit
196:Apple
193:class
181:Fruit
178:class
158:OCaml
654:Java
551:args
542:main
539:void
472:Uses
448:<
394:argv
388:char
379:argc
370:main
229:args
220:main
217:void
169:Java
117:base
599:();
593:new
424:();
418:new
376:int
367:int
364:{};
295:C++
256:();
250:new
149:).
119:or
103:In
778::
718:.
693:.
620:);
581:);
463:);
391:**
343:};
331:()
205:{}
184:{}
115:a
107:,
722:.
703:.
623:}
614:(
608:=
590:=
575:(
569:=
557:{
554:)
545:(
530:}
527:;
521:)
515:(
506:{
503:)
494:(
466:}
457:(
442:=
436:*
415:=
409:*
400:{
397:)
382:,
373:(
355::
340:;
334:=
325:~
316::
310:{
289:}
286:;
280:)
274:(
271:=
247:=
235:{
232:)
223:(
94:)
88:(
83:)
79:(
65:.
38:.
20:)
Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.