Source: Ch3_OOP.pdf

Encapsulation and Overloading

Access levels, method overloading, constructor overloading, and operator overloading.

Access Specifiers

Access specifiers control visibility: public (everywhere), private (class only), protected (class + subclasses), internal (same assembly). Use the most restrictive level that still works.

csharp
class Vehicle
{
    public string Brand = "Generic";     // accessible everywhere
    private int _mileage = 0;            // only inside this class
    protected string FuelType = "Gas";   // this class and subclasses

    public void Drive(int km) { _mileage += km; }
    public int GetMileage() => _mileage;
}

Vehicle v = new Vehicle();
v.Brand = "Toyota";
v.Drive(100);
Console.WriteLine(v.Brand + ": " + v.GetMileage() + " km");

Output

Toyota: 100 km

Access Specifiers — Exercise 1

Exposing internal state only through methods is the core of encapsulation — it lets you validate or log changes.

Fix the access violation

Hint

celsius is private. Use SetCelsius() so the validation runs.

Which specifier allows access within the class and in derived classes?

Method Overloading

Overloading defines multiple methods with the same name but different parameter lists. The compiler picks the correct version at compile time based on the arguments.

csharp
class Printer
{
    public void Print(string text)
    {
        Console.WriteLine(text);
    }

    public void Print(int number)
    {
        Console.WriteLine("Number: " + number);
    }

    public void Print(string text, int times)
    {
        for (int i = 0; i < times; i++)
            Console.WriteLine(text);
    }
}

Printer p = new Printer();
p.Print("Hello");
p.Print(42);
p.Print("Hi", 2);

Output

Hello
Number: 42
Hi
Hi

Method Overloading — Exercise 1

Overloaded methods must differ in the number or types of their parameters — differing only in return type is not enough.

Fix the invalid overload (same signature)

Hint

Two methods with the same name must differ in parameter types or count, not just return type.

What distinguishes two overloaded methods with the same name?

Constructor Overloading

A class can have multiple constructors with different parameter lists. Use : this(...) to delegate from one constructor to another to avoid repeating code.

csharp
public class Time
{
    public int Hour   { get; set; }
    public int Minute { get; set; }

    public Time() : this(0, 0) { }
    public Time(int h) : this(h, 0) { }
    public Time(int h, int m)
    {
        Hour = h;
        Minute = m;
    }

    public override string ToString() => $"{Hour:00}:{Minute:00}";
}

Console.WriteLine(new Time());
Console.WriteLine(new Time(9));
Console.WriteLine(new Time(14, 30));

Output

00:00
09:00
14:30

Constructor Overloading — Exercise 1

When two constructors share logic, delegate to the most complete one using : this(...) to avoid duplication.

Eliminate duplicated initialisation logic

Hint

Use : this(1, 1, 1) on the parameterless constructor to call the three-parameter one.

What syntax calls another constructor from within a constructor?

Operator Overloading

You can redefine what operators like + or == do for your class using the operator keyword. The method must be public and static.

csharp
class Vector2D
{
    public double X, Y;

    public Vector2D(double x, double y) { X = x; Y = y; }

    public static Vector2D operator +(Vector2D a, Vector2D b)
        => new Vector2D(a.X + b.X, a.Y + b.Y);

    public override string ToString() => $"({X}, {Y})";
}

Vector2D v1 = new Vector2D(1, 2);
Vector2D v2 = new Vector2D(3, 4);
Console.WriteLine(v1 + v2);

Output

(4, 6)

What modifiers are required on an overloaded operator method?