Search⌘ K
AI Features

Inheritance

Explore the principles of inheritance in C# to understand how base classes provide common functionality for derived classes. Learn how inheritance enables code reuse, the is-a relationship, and key constraints like single inheritance and access modifiers.

Overview

Inheritance is a fundamental concept of object-oriented programming. With inheritance, one class can inherit the functionality of another class. In other words, it allows programmers to encapsulate some common functionality in a base class, while more specific functionality will be implemented in child classes.

The best way to understand inheritance is to see it in action.

Let’s take a Car class as an example:

public class Car
{
	public string Model { get; set; }
	public decimal Price { get; set; }
	public int Year { get; set; }
	public int FuelCapacityInLiters { get; set; }
	public int FuelLeft { get; set; }

	public void Drive()
	{
	}
}

Our car has a model and price, along with other properties, and can be driven. What if we want to ...