Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Inheritance in C# and .NET
In this articleThis tutorial introduces you to inheritance in C#. Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality. Prerequisites
Running the examplesTo create and run the examples in this tutorial, you use the dotnet utility from the command line. Follow these steps for each example:
Background: What is inheritance?Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class. C# and .NET support single inheritance only. That is, a class can only inherit from a single class. However, inheritance is transitive, which allows you to define
an inheritance hierarchy for a set of types. In other words, type Not all members of a base class are inherited by derived classes. The following members are not inherited:
While all other members of a base class are inherited by derived classes, whether they are visible or not depends on their accessibility. A member's accessibility affects its visibility for derived classes as follows:
Derived classes can also override inherited members by providing an alternate implementation. In order to be able to override a member, the member in the base class must be marked with the virtual keyword. By default, base class members are not marked as
In some cases, a derived class must override the base class implementation. Base class members marked with the
abstract keyword require that derived classes override them. Attempting to compile the following example generates compiler error CS0534, "<class> does not implement inherited abstract member <member>", because class
Inheritance applies only to classes and interfaces. Other type categories (structs, delegates, and enums) do not support inheritance. Because of these rules, attempting to compile code like the following example produces compiler error CS0527: "Type 'ValueType' in interface list is not an interface." The error message indicates that, although you can define the interfaces that a struct implements, inheritance is not supported.
Implicit inheritanceBesides any types that they may inherit from through single inheritance, all types in the .NET type system implicitly inherit from Object or a type derived from it. The common functionality of Object is available to any type. To see what implicit inheritance means, let's define a new class,
You can then use reflection (which lets you inspect a type's metadata to get information about that type) to get a list of the members that belong to the
Implicit inheritance from the Object class makes these methods available to the
Because of implicit inheritance, you can call any inherited member from a
The following table lists the categories of types that you can create in C# and the types from which they implicitly inherit. Each base type makes a different set of members available through inheritance to implicitly derived types.
Inheritance and an "is a" relationshipOrdinarily, inheritance is used to express an "is a" relationship between a base class and one or more derived classes, where the derived classes are specialized versions of the base class; the derived class is a type of the base class. For example, the Note A class or struct can implement one or more interfaces. While interface implementation is often presented as a workaround for single inheritance or as a way of using inheritance with structs, it is intended to express a different relationship (a "can do" relationship) between an interface and its implementing type than inheritance. An interface defines a subset of functionality (such as the ability to test for equality, to compare or sort objects, or to support culture-sensitive parsing and formatting) that the interface makes available to its implementing types. Note that "is a" also expresses the relationship between a type and a specific instantiation of that type. In the following example,
In this case, you shouldn't rely on inheritance to represent specific car makes and models. For example, you don't need to define a
An is-a relationship based on inheritance is best applied to a base class and to derived classes that add additional members to the base class or that require additional functionality not present in the base class. Designing the base class and derived classesLet's look at the process of designing a base class and its derived classes. In this section, you'll define a base class, The base Publication classIn designing your
The following example shows the source code for the
The following figure illustrates the relationship between your base The Book classThe
In addition to the members that it inherits from
The following figure illustrates the relationship between the You can now instantiate a
Designing abstract base classes and their derived classesIn the previous example, you defined a base class that provided an implementation for a number of methods to allow derived classes to share code. In many cases, however, the base class is not expected to provide an implementation. Instead, the base class is an abstract class that declares abstract methods; it serves as a template that defines the members that each derived class must implement. Typically in an abstract base class,
the implementation of each derived type is unique to that type. You marked the class with the abstract keyword because it made no sense to instantiate a For example, each closed two-dimensional geometric shape includes two properties: area, the inner extent of the shape; and perimeter, or the distance along the edges of the shape. The way in which these properties are calculated,
however, depends completely on the specific shape. The formula for calculating the perimeter (or circumference) of a circle, for example, is different from that of a square. The The following example defines an abstract base class named
You can then derive some classes from
The following example uses objects derived from
FeedbackSubmit and view feedback for When an relationship exists between objects the specialized object?When an "is a" relationship exists between objects, it means the specialized object has all the characteristics of the general object, plus additional characteristics that make it special.
When an is a relationship exists between objects it means that the specialized object has quizlet?Terms in this set (61) When an "is a" relationship exists between objects, it means that the specialized object has: All of the characteristics of the general object, plus additional characteristics.
What object oriented programming characteristic allows you to create a class that is a specialized version of another class?Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
What is it called when one class inherits another class?Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.
|