The aim of this tutorial is to explain the concepts of Encapsulation and Abstraction in C# and how to implement them effectively.
Basic knowledge of C# programming language.
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.
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 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.
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.
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
.
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.
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.
To further improve your skills, practice these concepts by creating more complex classes and methods.
Person
class with encapsulation for Name
and Age
properties.Vehicle
class with an abstract method Move
and implement it in the Car
and Bicycle
classes.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; }
}
}
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.