Implementing Encapsulation and Abstraction

Tutorial 4 of 5

1. Introduction

Goal of the Tutorial

The aim of this tutorial is to explain the concepts of Encapsulation and Abstraction in C# and how to implement them effectively.

Learning Outcome

  • Understanding of what Encapsulation and Abstraction are.
  • How to hide data within classes using encapsulation.
  • How to define abstract classes and methods using abstraction.

Prerequisites

Basic knowledge of C# programming language.

2. Step-by-Step Guide

Encapsulation

Encapsulation is a concept of binding or encapsulating the data and the methods that manipulate the data into a single unit. In other words, it is a protective shield that prevents the data from being accessed by the code outside this shield.

Example:

public class Employee
{
    // private data member
    private string name;

    // public method to access and mutate private data member
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

In this example, name is a private data member encapsulated inside the Employee class. It can be accessed and mutated only through the public property Name.

Abstraction

Abstraction is a process of hiding the implementation details and showing only the functionality to the user. It lets you focus on what the object does instead of how it does it.

Example:

public abstract class Shape
{
    public abstract double Area();
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public override double Area()
    {
        return Width * Height;
    }
}

In this example, Shape is an abstract class and Area is an abstract method. The Rectangle class inherits Shape class and implements the Area method.

3. Code Examples

Encapsulation Example:

public class Student
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

// Usage
Student student = new Student();
student.Name = "John Doe"; // set operation
Console.WriteLine(student.Name); // get operation, print: John Doe

Here, _name is a private data member enclosed within the Student class. We access it using the public property Name.

Abstraction Example:

public abstract class Animal
{
    public abstract void Sound();
}

public class Dog : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Dog barks");
    }
}

// Usage
Animal myDog = new Dog();
myDog.Sound(); // print: Dog barks

Here, Sound is an abstract method of the abstract class Animal. The Dog class inherits Animal and implements the Sound method.

4. Summary

We've covered encapsulation and abstraction in C#. Encapsulation is about hiding and safeguarding data. Abstraction, on the other hand, is about hiding the implementation and exposing the functionality.

Next Steps

To further improve your skills, practice these concepts by creating more complex classes and methods.

Additional Resources

5. Practice Exercises

  1. Create a Person class with encapsulation for Name and Age properties.
  2. Create an abstract Vehicle class with an abstract method Move and implement it in the Car and Bicycle classes.

Solutions:

  1. Encapsulation Exercise:
public class Person
{
    private string _name;
    private int _age;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
}
  1. Abstraction Exercise:
public abstract class Vehicle
{
    public abstract void Move();
}

public class Car : Vehicle
{
    public override void Move()
    {
        Console.WriteLine("Car is driving");
    }
}

public class Bicycle : Vehicle
{
    public override void Move()
    {
        Console.WriteLine("Bicycle is pedaling");
    }
}

With these exercises, you should be able to grasp the concept of encapsulation and abstraction. Practice more to get familiar with these concepts.