Encapsulation in C# Classes

Encapsulation in C# Classes

Hello! In this lesson, we're revisiting Encapsulation, Private Attributes, and Private Methods in Object-Oriented Programming (OOP). Imagine encapsulation as an invisible fence safeguarding a garden from outside interference, keeping data and methods safe within. Within this garden, certain plants (Private Attributes and Methods) are only for the gardener's eyes. These are crucial for making your classes more robust and secure!

Encapsulation Explained

Encapsulation is a fundamental concept in Object-Oriented Programming (OOP) that involves bundling data (attributes) and methods (functions) that operate on the data into a single unit, called a class. This concept also restricts direct access to some of an object's components, thereby preventing accidental interference and misuse of the data. If you were to code a multiplayer game, for instance, you could create a Player class, encapsulating data (health, armor, stamina) and methods (ReceiveDamage, ShieldHit, RestoreHealth).

public class Player {
    private int health;
    private int armor;
    private int stamina;

    public Player(int health, int armor, int stamina) {
        this.health = health;
        this.armor = armor;
        this.stamina = stamina;
    }

    public void ReceiveDamage(int damage) {
        health -= damage;  // Reduce health
    }
    
    public void ShieldHit(int armorDamage) {
        armor -= armorDamage;  // Decrease armor
    }

    public void RestoreHealth(int healthIncrease) {
        health += healthIncrease;  // Restore health
    }
}

class Program {
    static void Main() {
        Player player = new Player(100, 50, 77);
    }
}

Now, player is an instance of the Player class on which you can call methods.

Remark the Privacy

In C#, the private keyword designates an attribute or method as limited to the class itself, meaning it cannot be accessed directly from outside the class. Note that the constructor itself cannot be private.

public class PrivateExample {
    private string privateAttribute;  // Declare a private field

    public string PublicAttribute { get; set; }

    public PrivateExample() {
        PublicAttribute = "Public";
        privateAttribute = "Private";  // Initialize private attribute
    }

    public string GetPrivateAttribute() {
        return privateAttribute;
    }
}

class Program {
    static void Main() {
        PrivateExample example = new PrivateExample();
        Console.WriteLine(example.PublicAttribute);  // Works: logs "Public"
        Console.WriteLine(example.GetPrivateAttribute());  // Works: logs "Private"
        // Console.WriteLine(example.privateAttribute);  // Error: can't access private attribute from outside
    }
}

Private attributes and methods are inaccessible directly from an instance. This arrangement helps maintain integrity.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal