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

In Chapter 6, we explained how classes derive from one another and described how classes can inherit properties and methods from their parent classes. In Chapter 7, you learned how to create your own classes and use objects of those classes, but you didn't see how the inheritance aspect works in practice. That's about to change.

We mentioned in Chapter 6 that the three key principles of object-oriented programming are encapsulation (discussed in Chapter 7), specialization, and polymorphism. This chapter focuses on specialization, which is implemented in C# through inheritance. You'll see how to create your own class hierarchy, and how to enforce that child classes implement the methods of their parent classes. You'll even see how to create completely abstract classes, and why you'd want to. You can't create an instance of an abstract class; you can only inherit from it. This chapter also explains how instances of a child class can be treated as though they were instances of one of the child class's ancestor classes, a process known as polymorphism. This chapter ends with a consideration of sealed classes, which cannot be specialized, and a discussion of the root of all classes, the Object class.

Specialization and Generalization

Before we can start to show you the syntax of inheritance, we first have to give you a little more object-oriented background, so you can see why inheritance works the way it does. Classes and their instances (objects) ...

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