C#encapsulation
C#encapsulation
specialized behavior
Article • 02/16/2022
Inheritance, together with encapsulation and polymorphism, is one of the three primary
characteristics of object-oriented programming. 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. A derived class can have only one direct base class.
However, inheritance is transitive. If ClassC is derived from ClassB , and ClassB is derived
from ClassA , ClassC inherits the members declared in ClassB and ClassA .
7 Note
Conceptually, a derived class is a specialization of the base class. For example, if you have a
base class Animal , you might have one derived class that is named Mammal and another
derived class that is named Reptile . A Mammal is an Animal , and a Reptile is an Animal ,
but each derived class represents different specializations of the base class.
Interface declarations may define a default implementation for its members. These
implementations are inherited by derived interfaces, and by classes that implement those
interfaces. For more information on default interface methods, see the article on interfaces.
When you define a class to derive from another class, the derived class implicitly gains all
the members of the base class, except for its constructors and finalizers. The derived class
reuses the code in the base class without having to reimplement it. You can add more
members in the derived class. The derived class extends the functionality of the base class.
The following illustration shows a class WorkItem that represents an item of work in some
business process. Like all classes, it derives from System.Object and inherits all its methods.
WorkItem adds six members of its own. These members include a constructor, because
constructors aren't inherited. Class ChangeRequest inherits from WorkItem and represents a
particular kind of work item. ChangeRequest adds two more members to the members that
it inherits from WorkItem and from Object. It must add its own constructor, and it also adds
originalItemID . Property originalItemID enables the ChangeRequest instance to be
associated with the original WorkItem to which the change request applies.
The following example shows how the class relationships demonstrated in the previous
illustration are expressed in C#. The example also shows how WorkItem overrides the virtual
method Object.ToString, and how the ChangeRequest class inherits the WorkItem
implementation of the method. The first block defines the classes:
//Properties.
protected int ID { get; set; }
protected string Title { get; set; }
protected string Description { get; set; }
protected TimeSpan jobLength { get; set; }
// Method Update enables you to update the title and job length of an
// existing WorkItem object.
public void Update(string title, TimeSpan joblen)
{
this.Title = title;
this.jobLength = joblen;
}
This next block shows how to use the base and derived classes:
Interfaces
An interface is a reference type that defines a set of members. All classes and structs that
implement that interface must implement that set of members. An interface may define a
default implementation for any or all of these members. A class can implement multiple
interfaces even though it can derive from only a single direct base class.
Interfaces are used to define specific capabilities for classes that don't necessarily have an
"is a" relationship. For example, the System.IEquatable<T> interface can be implemented
by any class or struct to determine whether two objects of the type are equivalent (however
the type defines equivalence). IEquatable<T> doesn't imply the same kind of "is a"
relationship that exists between a base class and a derived class (for example, a Mammal is
an Animal ). For more information, see Interfaces.
6 Collaborate with us on
GitHub .NET feedback
.NET is an open source project. Select a
The source for this content can
link to provide feedback:
be found on GitHub, where you
can also create and review issues
Open a documentation issue
and pull requests. For more
information, see our contributor
Provide product feedback
guide.