This chapter continues our discussion of object-oriented programming (OOP) by introducing one of its primary featuresinheritance, which is a form of software reuse in which a new class is created by absorbing an existing class's members and enhancing them with new or modified capabilities. With inheritance, programmers save time during application development by reusing proven and debugged high-quality software. This also increases the likelihood that a system will be implemented effectively.
When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. The existing class is called the base class, and the new class is the derived class. Each derived class can become the base class for future derived classes.
A derived class normally adds its own fields and methods. Therefore, a derived class is more specific than its base class and represents a more specialized group of objects. Typically, the derived class exhibits the behaviors of its base class and additional behaviors that are specific to the derived class.
The direct base class is the base class from which the derived class explicitly inherits. An indirect base class is any class above the direct base class in the class hierarchy, which defines the inheritance relationships among classes. The class hierarchy begins with class object (which is the C# alias for System.Object in the Framework Class Library), which every class directly or indirectly extends (or "inherits from"). Section 10.7 lists the methods of class object, which every other class inherits. In the case of single inheritance, a class is derived from one direct base class. C#, unlike C++, does not support multiple inheritance (which occurs when a class is derived from more than one direct base class). In Chapter 11, Polymorphism, Interfaces & Operator Overloading, we explain how you can use interfaces to realize many of the benefits of multiple inheritance while avoiding the associated problems.
Experience in building software systems indicates that significant amounts of code deal with closely related special cases. When programmers are preoccupied with special cases, the details can obscure the big picture. With object-oriented programming, programmers can, when appropriate, focus on the commonalities among objects in the system rather than the special cases.
We distinguish between the is-a relationship and the has-a relationship. Is-a represents inheritance. In an is-a relationship, an object of a derived class can also be treated as an object of its base class. For example, a car is a vehicle, and a truck is a vehicle. By contrast, has-a represents composition (see Chapter 9). In a has-a relationship, an object contains as members references to other objects. For example, a car has a steering wheel, and a car object has a reference to a steering wheel object.
New classes can inherit from classes in class libraries. Organizations develop their own class libraries and can take advantage of others available worldwide. Some day, most new software likely will be constructed from standardized reusable components, just as automobiles and most computer hardware are constructed today. This will facilitate the development of more powerful, abundant and economical software.
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types