C# – What is a derived class?

As the name implies, a derived class originates from another class, which in C# is called a base class.

A derived class is a specialization of the base class. A derived class is created from an existing base class. It inherits the member variables and methods of the base class from which it is derived.

For example, if you have a base class Music, you might have a derived class Classical and another derived class StringQuartet.

While Classical derives from Music, and StringQuartet derives from Music, each derived class can represent different specializations of the base class Music.

crone-park (24)

What is a derived class?

When you define a class to derive from another class, the derived class gains all the members of the base class – except its constructors and destructors.

This means the derived class can reuse the code in the base class without re-implementing it.

In the derived class, you can add more members allowing it to extend the functionality of the base class.

  • Base Class – members are inherited by the derived class.
  • Derived Class – inherits those members from the base class.

Each Derived Class Has Only One Direct Base Class

A derived class can have only one direct base class.

However, inheritance is transitive.

If

StringQuartet is derived from Classical

Classical is derived from Music

StringQuartet inherits the members declared in Classical and Music.

Polymorphism and Derived Classes

Polymorphism let us treat objects of a derived class as objects of its base class.

For example, big cats (base class) hunt (a method) in different ways. A Lion (derived class) stalks, while a Cheetah (another derived class) outruns it.

Virtual Methods

A virtual method is a type of method which can be overridden in a derived class.

Tip: use override sealed if you want a virtual method in a base class to become sealed in a derived class.

MSDN Definition

A class that was created based on a previously existing class (i.e., base class). A derived class inherits all of the member variables and methods of the base class from which it is derived.

C+ Derived Class Example

class Derived : [virtual] [access-specifier] Base

{

// member list

};

class Derived : [virtual] [access-specifier] Base1,

[virtual] [access-specifier] Base2, . . .

{

// member list

};

 

Note: In C#, both inheritance and interface implementation are defined by the : operator, equivalent to extends and implements in Java. The base class should always be rightmost in the class declaration.

Single Inheritance

In “single inheritance,” a form of inheritance, classes have only one base class.

Consider the relationship illustrated in the following figure.

PrintedBook

Book

PaperbackBook

Note the progression from general (PrintedBook) to specific (PaperbackBook).

Kind of Relationships

The derived class has a “kind of” relationship with the base class.

Why?

A Book is a kind of a PrintedDocument

A PaperbackBook is a kind of a book.

Book is

both a derived class (from PrintedDocument) and a

base class (as PaperbackBook is derived from Book).

Code Sample

class PrintedDocument {};

// Book is derived from PrintedDocument.

class Book : public PrintedDocument {};

// PaperbackBook is derived from Book.

class PaperbackBook : public Book {};

Direct Base v Indirect Base

PrintedDocument is a “direct base” class to Book.

PrintedDocument is an “indirect base” class to PaperbackBook.

Why?

A direct base class appears in the base list of a class declaration – an indirect base does not.

The base class from which each class is derived is declared before the declaration of the derived class.

Learn more at http://msdn.microsoft.com

Have you got these templates?