Inheritance and Polymorphism—Exercises

Jump to: navigation, search
Visual C# Tutorials

Inheritance and Polymorphism

© 2006 O'Reilly Media, Inc.


Exercise 11-1. Create a base class, Telephone, and derive a class ElectronicPhone from it. In Telephone, create a protected string member phonetype, and a public method Ring( ) that outputs a text message like this: "Ringing the <phonetype>." In ElectronicPhone, the constructor should set the phonetype to "Digital." In the Run( ) method, call Ring( ) on the ElectronicPhone to test the inheritance.

Exercise 11-2. Extend Exercise 11-1 to illustrate a polymorphic method. Have the derived class override the Ring( ) method to display a different message.

Exercise 11-3. Change the Telephone class to abstract, and make Ring( ) an abstract method. Derive two new classes from Telephone: DigitalPhone and TalkingPhone. Each derived class should set the phonetype, and override the Ring( ) method.

prevpp.png  nextpp.png
C# Online.NET