What are Base and Derived Classes in C#?

In C# object-oriented programming, inheritance allows you to create new classes based on existing ones. A base class (also called parent class) is the original class that provides common properties and methods. A derived class (also called child class) inherits from the base class and can access its members while adding its own specific functionality.

The derived class automatically inherits all accessible members from the base class, including fields, properties, and methods, promoting code reusability and establishing an "is-a" relationship.

Syntax

Following is the syntax for creating a derived class in C# −

class BaseClass {
   // base class members
}

class DerivedClass : BaseClass {
   // derived class members
   // inherits all accessible members from BaseClass
}

Base and Derived Class Relationship Vehicle Base Class Truck Derived Class Bus Derived Class Motorbike Derived Class

Using Base and Derived Classes

Example

The following example demonstrates inheritance where Rectangle derives from Shape and inherits its properties and methods −

using System;

class Shape {
   public void setWidth(int w) {
      width = w;
   }
   public void setHeight(int h) {
      height = h;
   }
   protected int width;
   protected int height;
}

// Derived class
class Rectangle: Shape {
   public int getArea() {
      return (width * height);
   }
}

class Demo {
   static void Main(string[] args) {
      Rectangle Rect = new Rectangle();
      Rect.setWidth(5);
      Rect.setHeight(7);
      // Print the area of the object.
      Console.WriteLine("Total area: {0}", Rect.getArea());
   }
}

The output of the above code is −

Total area: 35

Multiple Inheritance Through Interfaces

C# supports single class inheritance but allows multiple inheritance through interfaces. A class can inherit from only one base class but can implement multiple interfaces −

Example

using System;

interface IMovable {
   void Move();
}

interface IFlyable {
   void Fly();
}

class Vehicle {
   public string Brand { get; set; }
   public void Start() {
      Console.WriteLine("Vehicle started");
   }
}

class Airplane : Vehicle, IMovable, IFlyable {
   public void Move() {
      Console.WriteLine("Airplane is moving on ground");
   }
   
   public void Fly() {
      Console.WriteLine("Airplane is flying in the air");
   }
}

class Program {
   static void Main(string[] args) {
      Airplane plane = new Airplane();
      plane.Brand = "Boeing";
      plane.Start();  // inherited from Vehicle
      plane.Move();   // implemented from IMovable
      plane.Fly();    // implemented from IFlyable
   }
}

The output of the above code is −

Vehicle started
Airplane is moving on ground
Airplane is flying in the air

Access Modifiers in Inheritance

Access Modifier Accessible in Derived Class
public Yes
protected Yes
private No
internal Yes (within same assembly)

Conclusion

Base and derived classes in C# enable inheritance, allowing derived classes to reuse code from base classes while extending functionality. C# supports single class inheritance but allows implementing multiple interfaces, providing flexibility in object-oriented design patterns.

Updated on: 2026-03-17T07:04:35+05:30

380 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements