O'Reilly logo

Learning C# 3.0 by Brian MacDonald, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required