Classes, Objects & inheritance
Master object-oriented programming concepts including classes, objects, and inheritance in C#
70 min•By Priygop Team•Last updated: Feb 2026
Understanding Classes and Objects
Classes are blueprints for creating objects. They define the structure and behavior that objects of that class will have. Objects are instances of classes that contain actual data and can perform actions.
Class Definition and Object Creation
Example
// Class definition
public class Car
{
// Fields (attributes)
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double Speed { get; set; }
// Constructor
public Car(string brand, string model, int year)
{
Brand = brand;
Model = model;
Year = year;
Speed = 0;
}
// Methods (behavior)
public void Accelerate(double increment)
{
Speed += increment;
Console.WriteLine($"{Brand} {Model} is now going {Speed} km/h");
}
public void Brake(double decrement)
{
Speed = Math.Max(0, Speed - decrement);
Console.WriteLine($"{Brand} {Model} slowed down to {Speed} km/h");
}
public void DisplayInfo()
{
Console.WriteLine($"Car: {Year} {Brand} {Model}");
}
}
// Creating objects
Car myCar = new Car("Toyota", "Camry", 2023);
Car yourCar = new Car("Honda", "Civic", 2022);
// Using objects
myCar.DisplayInfo();
myCar.Accelerate(50);
yourCar.Accelerate(30);