Outcast Downcasting
suggest changeBeware of using “downcasting”. Downcasting is casting down the inheritance hierarchy from a base class to a subclass (the opposite of an upcast). In general, use polymorphism and overriding instead of instanceof and downcasting.
C++ Example
// explicit type cast required
Child *pChild = (Child *) &parent;
Java Example
if (someBaseClass instanceof SubClass) {
SubClass mySubClass = (SubClass) someBaseClass;
mySubClass.nonInheritedMethod();
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents