Please enable JavaScript.
Coggle requires JavaScript to display documents.
08 Inheritance the need for casting (Best practice (Only use…
08
Inheritance
the need for casting
Class and
member
modifiers
Protected
Class
Member
Only accessible from derived class
Sealed
Class (cannot derive from it)
Overridden virtual member
(cannot override further in derived class)
Structs and enums automatically sealed
Invoking
base class
members
Use "base dot" notation
Calls first matching method
going up the class hierarchy
Casting
Upcasting
Automatic, no extra syntax required
Always safe
Object of derived class used
in place of object of base class
Downcasting
Object of base class used
in place of object of derived type
Not always safe
Requires explicit (...) cast syntax
"is" operator says if it will work
"as" operator casts if OK or gives null if not OK
Best
practice
Only use inheritance for real "is a" relationship
Inheritance is a serious commitment
(don't use for ad-hoc quick fixes)
C# limited to single inheritance
Labs
Circular things - enhancements