Back to course

Inheritance: Reusing Code

.NET Zero to Hero: Master C# and Modern App Development

Inheritance

Inheritance allows a class (child) to inherit fields and methods from another class (parent).

Syntax:

csharp class Vehicle { // Parent public string brand = "Ford"; public void Honk() => Console.WriteLine("Beep!"); }

class Car : Vehicle { // Child public string model = "Mustang"; }

Car now has access to the brand field and Honk() method.