Skip to main content

C# Inherited Classes (Derived Classes)

In C#, inheritance is a fundamental concept that allows you to create new classes (derived or child classes) based on existing classes (base or parent classes). It enables you to achieve abstraction and build a hierarchy of classes. One essential aspect of inheritance is the use of abstract classes.

Abstract Classes

An abstract class is a class that cannot be instantiated directly. Instead, it serves as a blueprint for other classes, defining rules and requirements that must be followed by derived classes. To declare an abstract class:

  • It must have at least one abstract method (a method without implementation).
  • It is marked with the abstract keyword within the class definition.

Derived classes (classes that inherit from an abstract class) are obligated to provide implementations for the abstract methods defined in the base abstract class.

Creating an Inherited Class

Here's an example of creating an inherited class in C#:

public class QueryRequest
{
// Properties for query
public int? from { get; set; }
public int? size { get; set; }
public int? memberId { get; set; }
public List<Filter> filters { get; set; }
public Sort sort { get; set; }
}

public class QueryRequestWithQ : QueryRequest
{
// Additional property for query
public string q { get; set; }
}