Chapter 11: Inheritance and Polymorphism

Quiz Solutions

Solution to Question 11-1. This relationship between specialization and generalization is reciprocal: if you have three types with similar functionality, you can factor that similarity out of the three types into a generalized type. At the same time, the three types are now specialized forms of the more generalized type.

Inheritance is also implicitly hierarchical: you can imagine a tree with the most generalized type at the top and each level of specialization descending from levels above. A generalized type may have many specializations, but each specialized type may have only one generalization.

Solution to Question 11-2. In C#, the principle of specialization is implemented through inheritance.

Solution to Question 11-3. The syntax for inheritance is:

class <identifier> : <base class>

Solution to Question 11-4. To implement polymorphism, you create a virtual method in the base class, and then override it in the derived class.

Solution to Question 11-5. The more usual meaning of new is to allocate memory on the heap. The special meaning in inheritance is that you are not overriding a base method; you are creating a new method that intentionally hides and replaces the base class method.

Solution to Question 11-6. To call a base class constructor from a derived class, after the parameter list but before the opening brace put a colon followed by the keyword base and two parentheses. Pass the parameters for the base class constructor ...

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.