Close
Close

Abstraction in C#


Abstraction is basically showing the required feature to a user and hiding the implementation and detail. With abstraction, a user can implement and new functionality to a program without going into details of it.

For example, think of a machine to make curd. The machine takes milk and makes curd from it. We don't know the detail of how the machine is working but we can use the curd for different purposes. This is abstraction.

In C#, we can achieve abstraction in two ways - by using abstract classes and by using interface. Let's learn about each of these.

C# Abstract Classes


An abstract class is a class which must contain at least one abstract method. An abstract method is a method which is declared using abstract keyword and has an empty body. Its definition is defined in the subclasses.

An abstract class can contain any number of abstract and non-abstract methods but it must contain at least one abstract method. We can't create instances (objects) of an abstract class but we can make objects of its subclasses (if they are not an abstract). Let's take an example.

Let's take an example of a firm. The firm hires only two types of employees- either driver or developer. Now, you have to develop a software to store the information about them.

So, here is an idea - There is no need to make objects of the employee class. We will make objects of only driver or developer. Also, both must have some salary. So, there must be a common method to know about the salary.

So, we can make Employee an abstract class and Developer and Driver its subclasses.

using System;

abstract class Employee //abstract class
{
  public abstract int GetSalary(); //abstract method
}

class Developer: Employee
{
  private int salary;
  public Developer(int s)
  {
    salary = s;
  }
  public override int GetSalary()
  {
    return salary;
  }
}

class Driver: Employee
{
  private int salary;
  public Driver(int s)
  {
    salary = s;
  }
  public override int GetSalary()
  {
    return salary;
  }
}

class Test
{
  static void Main(string[] args)
  {
    Developer d1 = new Developer(5000);
    Driver d2 = new Driver(3000);

    int a, b;

    a = d1.GetSalary();
    b = d2.GetSalary();
    Console.WriteLine($"Salary of developer : {a}");
    Console.WriteLine($"Salary of driver : {b}");
  }
}
Output
Salary of developer : 5000
Salary of driver : 3000

abstract class Employee → Employee is an abstract class as 'abstract' keyword is used.

Since, Employee is an abstract class so, there must be an abstract method in it. It is GetSalary().

abstract int GetSalary(); → Notice that it has no nody and its body has to be defined in its subclasses and 'abstract' keyword is also used here.

In both the subclasses, GetSalary method is defined.

Let's look at one more example.

using System;

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

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

class Cat: Animal
{
  public override void Sound()
  {
    Console.WriteLine("Cats meow");
  }
}

class Monkey: Animal
{
  public override void Sound()
  {
    Console.WriteLine("Monkeys whoop");
  }
}

class Test
{
  static void Main(string[] args)
  {
    Animal d = new Dog();
    Animal c = new Cat();
    Animal m = new Monkey();
    d.Sound();
    c.Sound();
    m.Sound();
  }
}
Output
Dogs bark
Cats meow
Monkeys whoop

C# Interface


An interface is similar to an abstract class but all of its methods are abstract by default. It means that it can't have non-abstract methods. We use interface keyword to define an interface.

Since all the methods of an iterface are abstract by default, so we don't use public abstract before method declarations.

Let's look at an example.

using System;

interface Shape
{
  int GetArea();
  int GetPerimeter();
}

class Rectangle: Shape
{
  int length;
  int breadth;

  public Rectangle(int l, int b)
  {
    length = l;
    breadth = b;
  }

  public int GetArea()
  {
    return length*breadth;
  }

  public int GetPerimeter()
  {
    return 2*(length+breadth);
  }
}

class Square: Shape
{
  int side;

  public Square(int s)
  {
    side = s;
  }

  public int GetArea()
  {
    return side*side;
  }

  public int GetPerimeter()
  {
    return 4*side;
  }
}

class Test
{
  static void Main(string[] args)
  {
    Rectangle r = new Rectangle(7, 4);
    Square s = new Square(4);

    Console.WriteLine("Rectangle :");
    Console.WriteLine($"Area : {r.GetArea()} Perimeter : {r.GetPerimeter()}");

    Console.WriteLine("Square :");
    Console.WriteLine($"Area : {s.GetArea()} Perimeter : {s.GetPerimeter()}");
  }
}
Output
Rectangle :
Area : 28 Perimeter : 22
Square :
Area : 16 Perimeter : 16

Here, we have declared Shape as an interface using the interface keyword. Both methods inside it are by default public and abstract.


Ask Yours
Post Yours
Doubt? Ask question