Abstraction and Abstract Class in C#

What is abstraction and how to use abstract classes

Category: C# Tags: C#


    Abstraction is required to hide implementation details of a method/class and expose only required information to user. Like in Microsoft's base class library there are many inbuilt functions and classes, we can use them, we know their required parameters and expected outputs but we can't see their implementation details.


Abstract Class :- an abstract class is an incomplete class that can have declared as well as defined members or methods. Some key things regarding abstract classes are:


  1. Abstract class is a class that can have 0 or more members as well as defined and declared members.

  2. it forces us to implement declared members in derived class. if we don't implement them in derived class then we have to create derived class as abstract.

  3. we can not create object of abstract class.
  4. we use abstract classes when we have some incomplete members in a class.
  5. we can have virtual methods in abstract class.
  6. we can not use both virtual and abstract keywords same class. The abstract members works like virtual members in an abstract class.


Let's create an abstract class:

public abstract class Computer
public string Processor;
public int RAM;

/// <summary>
/// Virtual method can be overriden in child
/// </summary>
/// <param name="processor"></param>
public virtual void SetProcessor(string processor)
this.Processor = processor;
/// <summary>
/// Abstract method must be overriden in child
/// </summary>
/// <param name="ram"></param>
public abstract void SetRAM(int ram);

        We can see there the members of an abstract class may be complete or incomplete also. Now let me create a child class of this abstract class because there is no use of a abstract class if we don't create a concrete class, since abstract classes can not be instantiated.

public class Dell : Computer
/// <summary>
/// Overriden abstract method
/// </summary>
/// <param name="ram"></param>
public override void SetRAM(int ram)
base.RAM = ram;

        Above we created a simple class that inheriting the abstract class. we are implementing here the incomplete method SetRAM of abstract class Computer. Now let's run above classes:


static void Main(string[] args)
Computer computer = new Dell();

Console.WriteLine("Processor: " + computer.Processor);
Console.WriteLine("RAM: " + computer.RAM + " GB");

Output :-

Processor: i3


Like 0 People
Last modified on 30 September 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 126
Questions: 9
Given Best Solutions: 8 *


No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User