WPF: Using C# Interfaces to Create Polymorphic Classes
When a class is derived from another class, commonly known as a Base Class or Super Class it inherits all of its members (Methods, Events, Properties, etc.). But say the derived class needs to modify what it has inherited? There are two ways to accomplish this: The members can be replaced or the derived class can override its members (provided the base members are virtual...Don't let this confuse you; I will explain in later). If you replace a base class member you have to use the new keyword. Personally, I don't feel that this is best practice because I am of the opinion that if you have to replace a member of the base class there is a fundamental flaw in the design of the architecture but that is another article for another time; just take my word for it for now. In this article I am going to explain to you how to:
- Use Interfaces to declare methods;
- Create a base class that declares and/or defines those methods;
- Create classes that are derived from the base class;
- Have the derived classes override the base class methods;
- In the base class show you how to require that the derived child classes override the base class methods;
Victor is a senior application engineer at IdentityMine, a top-tier software company specializing in designing and developing cutting edge software applications, creating customized WPF/Blend and Silverlight training programs and creating tools for designers and developers. Victor has worked with Fortune 500 companies such as Microsoft, Harrah's, Mattel, NBC/Universal, Disney, Best Buy, GameStop, Vivendi Universal Games, and New Line Cinema among others. Victor, a former Flash/ActionScript engineer still stays active in the Flash community by writing articles and tutorials on www.actionscript.org where he is also a moderator.