Simple, Quality, Awesome Software

Polymorphism Quiz

Below are the answers to the Polymorphism Quiz, along with a brief discussion.

1. True. That’s the goal of polymorphism. While a base class may provide a certain implementation for a method, polymorphism means that derived types can substitute their own implementation for the method in it’s place. In C#, polymorphism is done by using the virtual keyword on a method in the base class and the override keyword in a derived class.

2. True.

3. False. The new keyword, when used on a method, means you’re creating a completely unrelated new method that happens to have the same name. It is discouraged except when required.

4. False. abstract methods can only be included in abstract classes.

5. True. An abstract class can have anything a normal class can have, plus abstract methods are allowed. You are just unable to actually create instances of the abstract class. Instead, you need to create instances of a derived class.

6. True.

7. True. In fact, assuming that the class is not abstract itself, it must override any abstract methods.

8. False. C#’s policy is that you cannot override methods that weren’t indicated as such in the base class with either abstract or virtual. This gives the base class the ability to be included in the decision of whether alternate versions of the method should be allowed.