Chapter 7: Inheritance and Polymorphism


Overview

Inheritance and polymorphism are two concepts that drive object-oriented design and development. Traditionally, all object-oriented languages support both of these concepts; for C# the syntax is similar to C++.

Inheritance allows class hierarchies to be formed. Class hierarchies are groups of classes that are related because they share certain things in common. For example, we could define a class hierarchy based on mammals where we could introduce human, monkey, and mouse as three different types of mammal. Each of these mammals is different but they are basically of the type ‘mammal’. Hence, we can create a class hierarchy to model the behavior of mammals with a Mammal class at the top, which is referred to as the base class, and the other three classes as derived classes. The derived classes will not only inherit all the members of the base class (including its properties, fields, and methods, as long as these are not private to the base class) but will also have the ability to add to the behavior of the Mammal class that they have inherited from. For example, the Human class may have properties or methods that are unique to humans, like Job Title, or Telephone Number, and the Monkey class may have a Tree property that is unique to a monkey and specifies the location of the monkey's tree. In short, all of these classes would have the common elements attributed to all mammals, but specialized to include their own capabilities and attributes.

Inheritance avoids code duplication, and enables code reuse, which means that each class doesn't require its own copy of the Mammal class implementation. As a result we have also created a hierarchy of classes that can be used to illustrate class relationships in code. We can use inheritance and create categories of things and relationships and this enables us to break complex problems into more manageable chunks.

Polymorphism is enabled by inheritance. Because all of our derived classes inherit the basic functionality of the base class, we can treat instances of the derived classes as if they are an instance of the base class. This is the principle of substitutability – we can ‘swap out’ an instance of a base class, and substitute for it an instance of a derived class, and code will continue to work – to be able to call methods and access attributes of the instance that it would expect to access on the base class. The ability of an object of one type to be treated as if it were an instance of another type is called polymorphism.

One additional capability inheritance affords derived classes is that they can override the default base class implementation and provide their own implementation of properties or methods. In these cases, code that is designed to access functionality on a base class may find an instance of the base class ‘substituted’ with an instance of a different, derived class, and may then find that calling a method on the base class will cause code in the derived class to be executed. This is enabled in .NET by a technique called virtual method dispatching, and we'll look at how it works later on in the chapter. Sometimes, we may want to prevent derived classes from overriding functionality; other times, it may be crucial for them to do so. In this chapter, we'll see how we can control inheritance, and

In .NET, as in most object-oriented languages, we generally refer to the parent class as a base class and all of the child classes as derived classes. We can enable polymorphism in two ways – by defining an interface that is implemented by the derived class, or by inheritance and method overriding, which enables a new implementation of an inherited method. Other programming languages use different terminology for this, for example, Java often calls the base class a superclass and the derived class a subclass. Subclassing is a popular term among C++ developers to describe deriving from a base class and extending its operations and attributes.

Important

Throughout this chapter we may use the terms operations and attributes interchangeably with the terms methods and fields; the former refer to the latter on a conceptual level and the latter are the terms we use to denote an implementation of these concepts in code.

In the sections that follow we will describe some of the key concepts behind inheritance and polymorphism without using code, to enable a fuller understanding at a more conceptual level.




C# Class Design Handbook(c) Coding Effective Classes
C# Class Design Handbook: Coding Effective Classes
ISBN: 1590592573
EAN: 2147483647
Year: N/A
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net