Practical 5 - Encapsulation

CAB201 - Programming Principles

School of Computer Science, Faculty of Science

Encapsulation in C#

Basically a class. In programming, encapsulation refers to the bundling of data into logical units that are ideally responsible for their own state (fields) and behaviour (methods).

Usually accompanied by information hiding using access modifiers to restrict unwanted access to the data.

public class Circle {

}
public class Circle {
    public double radius; // bad practice
}
public class Circle {
    private double radius; // private: only accessible within the class
}
public class Circle {
    private double radius; // private: only accessible within the class
    public double GetRadius() { return radius; } // public: accessible anywhere
}
public class Circle {
    private double radius; // private: only accessible within the class
    public double GetRadius() { return radius; } // public: accessible anywhere
    public double Area() { return Math.PI * radius * radius; }
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Accessing private fields - In other languages

Book.cs defines a class with a private field.

class Book {
    private string title;
}
class Book {
    private string title;
    public void SetTitle(string title) {
        this.title = title;
    }
}
class Book {
    private string title;
    public void SetTitle(string title) {
        this.title = title;
    }
    public string GetTitle() {
        return title;
    }
}
class Book {
    private string title;
    public void SetTitle(string title) {
        if (title.Length > 0) this.title = title; 
        else Console.WriteLine("Cannot be empty");
    }
    public string GetTitle() {
        return title;
    }
}

Main.cs class, trying to access the private field.

Book myBook = new Book();
Book myBook = new Book();
myBook.SetTitle("Another C# Book");
Book myBook = new Book();
myBook.SetTitle("Another C# Book");
Console.WriteLine(myBook.GetTitle());
Book myBook = new Book();
myBook.SetTitle("");
Console.WriteLine(myBook.GetTitle()); 
// Cannot be empty
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Accessing private fields - In C#

Book.cs defines a class with a private field.

class Book {
    private string title;
}
class Book {
    private string title;
    public string Title { // Property
        set {
            title = value;
        }
    }
}
class Book {
    private string title;
    public string Title { // Property
        set {
            title = value;
        }
        get {
            return title;
        }
    }
}
class Book {
    private string title;
    public string Title { // Property
        set {
            if (value.Length > 0) title = value;
            else Console.WriteLine("Cannot be empty");
        }
        get {
            return title;
        }
    }
}

Main.cs class, trying to access the private field.

Book myBook = new Book();
Book myBook = new Book();
myBook.Title = "Another C# Book";
Book myBook = new Book();
myBook.Title = "Another C# Book";
Console.WriteLine(myBook.Title);
Book myBook = new Book();
myBook.Title = "";
Console.WriteLine(myBook.Title);
// Cannot be empty
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

**Properties (C# approach)** ```csharp class Book { private string title; public string Title { get { return title; } set { if (value.Length > 0) title = value; else Console.WriteLine("Cannot be empty"); } } } ``` ```csharp Book myBook = new Book(); myBook.Title = "Another C# Book"; Console.WriteLine(myBook.Title); ```