C# & .NET Framework Advanced OOP — Mastering the Craft
💡
Exercise 36

Inheritance — Building on Foundations 15 XP Medium

Ctrl+Enter Run Ctrl+S Save

🏗️ Inheritance: Standing on the Shoulders of Giants

Inheritance is one of the four pillars of Object-Oriented Programming. It lets a derived class (child) inherit fields, properties, and methods from a base class (parent) — so you write common logic once and reuse it everywhere.

Think of it like architecture: every building has a foundation, walls, and a roof. A skyscraper is a building with extra floors and elevators. A house is a building with a garden and a porch. They share the core "building" blueprint but extend it in their own way.

The : Syntax — Declaring Inheritance

In C#, a class inherits from another using the colon : symbol. C# supports single inheritance only — a class can have exactly one direct base class (but can implement many interfaces).

class Animal // Base class (parent) { public string Name; public void Eat() => Console.WriteLine($"{Name} is eating."); } class Dog : Animal // Derived class (child) { public void Bark() => Console.WriteLine($"{Name} says Woof!"); } // Usage: Dog d = new Dog(); d.Name = "Rex"; // Inherited from Animal d.Eat(); // Inherited from Animal d.Bark(); // Defined in Dog

Constructor Chaining with base()

When you create a derived object, the base class constructor runs first. If the base constructor requires parameters, you must call it explicitly using the base() keyword in the derived constructor.

class Building { public string Name; public Building(string name) { Name = name; Console.WriteLine("Building constructed!"); } } class Skyscraper : Building { public int Floors; // Calls base class constructor first, then runs its own public Skyscraper(string name, int floors) : base(name) { Floors = floors; } public void Describe() { Console.WriteLine($"Skyscraper: {Name} ({Floors} floors)"); } }

Method Inheritance — What You Get for Free

A derived class automatically inherits all public and protected members of the base class. private members exist in the derived object's memory but are not directly accessible.

  • public — accessible everywhere, inherited and visible in derived classes
  • protected — accessible within the class and its derived classes only
  • private — accessible only within the declaring class (not even children)
  • base() — calls the parent constructor; must match a parent constructor signature
  • Single inheritance — a class can extend exactly one base class in C#

🏛️ The Architecture Analogy

Imagine the city planning office has a master Building blueprint. Every structure in the city — skyscrapers, houses, malls — starts from that blueprint. Each adds its own features (floors, gardens, parking lots) but the foundation is shared. That's inheritance: write it once in the base, reuse it in every derived class.

// Complete hierarchy example class Vehicle { public string Brand; protected int Year; // accessible in derived classes public Vehicle(string brand, int year) { Brand = brand; Year = year; } public void ShowInfo() { Console.WriteLine($"{Brand} ({Year})"); } } class Car : Vehicle { public int Doors; public Car(string brand, int year, int doors) : base(brand, year) { Doors = doors; } public void ShowFull() { ShowInfo(); // inherited method Console.WriteLine($"Doors: {Doors}, Year: {Year}"); // Year is protected, accessible here } }
📋 Instructions
**Your Mission: Build the City Hierarchy!** Create a `Building` base class and two derived classes — `Skyscraper` and `House`. 1. Create a `Building` class with: - A `public string Name` field - A constructor that takes `string name`, assigns it, and prints `"Building constructed!"` - A method `void Identify()` that prints `"Both are buildings!"` 2. Create a `Skyscraper` class inheriting from `Building`: - An `int Floors` field - A constructor `Skyscraper(string name, int floors)` that calls `base(name)` and sets `Floors` - A method `void Describe()` that prints `"Skyscraper: {Name} ({Floors} floors)"` 3. Create a `House` class inheriting from `Building`: - An `int Floors` field - A constructor `House(string name, int floors)` that calls `base(name)` and sets `Floors` - A method `void Describe()` that prints `"House: {Name} ({Floors} floors)"` 4. In `Main`, create a `Skyscraper("Burj Tower", 100)` and call `Describe()`, then create a `House("Cozy Cottage", 2)` and call `Describe()`, then call `Identify()` on either object.
Remember: derived constructors must call `base(name)` to pass the name to the Building constructor. The Building constructor prints "Building constructed!" each time a derived object is created. Use `: base(name)` after the derived constructor's parameter list.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.