Chapter 5. Inheritance and Polymorphism
The preceding chapter demonstrated how to create new types by declaring classes. This chapter explores the relationship between objects in the real world and how to model these relationships in your code. This chapter focuses on specialization, which is implemented in C# through inheritance. This chapter also explains how instances of more specialized types can be treated as though they were instances of more general types, a process known as polymorphism. This chapter ends with a consideration of sealed classes, which can't be specialized; abstract classes, which exist only to be specialized; and the root of all classes, the Object
type.
Tip
VB 6 programmers take note: like VB.NET, C# provides full object-oriented technology, including inheritance, polymorphism, and encapsulation. These are relatively new topics for VB 6 programmers. You should study them carefully; they affect your class and application design.
Specialization and Generalization
Classes and their instances (objects) don't exist in a vacuum, but rather in a network of interdependencies and relationships, just as we, as social animals, live in a world of relationships and categories.
The is-a relationship is one of specialization. When we say that a dog is-a mammal, we mean that the dog is a specialized kind of mammal. It has all the characteristics of any mammal (it bears live young, nurses with milk, has hair), but it specializes these characteristics to the familiar characteristics ...
Get Programming C# 3.0, 5th Edition 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.