Recent Blogs‎ > ‎

Difference between Abstract class and Interface - C#

posted Oct 14, 2009, 11:16 PM by Karthik Karuppasamy   [ updated Jun 21, 2018, 3:10 AM ]
Every C# developer would have faced this question at least once in their development career. If you are a fresher, hopefully following real time example could be helpful for you to understand them. Please note that this example is only for understanding the concept and could suit approximately and not appropriately.

Assume you have to purchase shirts for your brother and cousin for upcoming traditional function. You have only Rs.10000 in your hand and hence you are fixing Rs.5000 per head for the shirt. Now the problem is, you don't know anything about their liking and disliking. So you have decided to give the amount to choose of their own. 

Points to Consider: 
  1. As it is traditional function, you want them to buy formal shirt only and not T-Shirt. 
  2. You have to make sure that they will use the whole amount for the shirt and should not use for other stuffs
  3. Also you want them to decide their own brand and colour of the shirt. 
Eventually you decided to provide Generic Gift Voucher from Bank or ShowRoom to buy the shirt.The Generic Gift voucher is so called "Interface" which makes sure that the shirts "[of their own choices]" are bought within the money range.

Below code represents the IGiftVoucher5000 interface and Shirt class. The "Shirt" return type for GetShirt() function in Interface, makes sure that shirt has only been bought. 

//Interface which is similar to GiftVoucher
interface IGiftVoucher5000
{//Return type "Shirt" class make sure that they buy shirt only
Shirt GetShirt();
}
public class Shirt
{
public Shirt(string brandName, string color)
{
this.Brand = brandName;
this.Color = color;
}
public string Brand { get; set; }
public string Color { get; set; }
}

Now your brother and cousin can use that gift voucher and can buy shirt with his own choice of "brand". Similarly "Brother" class can inherit the interface "IGiftVoucher5000" and implement "GetShirt()" of his own. Also "Cousin" can inherit and implement "GetShirt()" of his own.
Note: GetShirt() function does not have any implementation in interface. Inheriting class has to implement it. If it is not implemented then it will throw an error.
public class Cousin : IGiftVoucher5000
{// Cousin can implement his own choice of brand and color
public Shirt GetShirt()
{
   return new Shirt("Peter England", "Black");
}
}
public class Brother : IGiftVoucher5000
{// Brother also can mention his own choice
public Shirt GetShirt()
{
return new Shirt("Raymond", "White");
}
}
class Program
{
static void Main(string[] args)
{
Cousin Ram = new Cousin();
Console.WriteLine("Your cousin bought {0} : {1} shirt of his own choice !!!", Ram.GetShirt().Brand, Ram.GetShirt().Color);
Brother Sri = new Brother();
Console.WriteLine("Your brother bought {0} : {1} shirt of his own choice !!!", Sri.GetShirt().Brand, Sri.GetShirt().Color);
Console.ReadLine();
}
}

Now create an object for Brother, Cousin classes and call the GetShirt(). The output will be
Your cousin bought Peter England : Black shirt of his own choice !!! Your brother bought Raymond : White shirt of his own choice !!!
Bit simple, isn't it?? Now it's the time to move on for "Abstract". In our above example, you let your brother and cousin to make their own choice while buying the shirt. But now you want to present the shirt from your favorite and best brand as default option. So you have decided to opt for "The Raymond Shop - Gift card" instead of Generic gift voucher which will have additional Check. In the below abstract class, we can show alert message if some other brand is passed.
This "The Raymond Shop - Gift card" is so called Abstract class which has default check for GetShirt() using it's default implementation and still forces the subclasses to follow certain protocol.
public abstract class GiftVoucher5000
{
//Constant for Raymond
private const string BrandName = "Raymond";
//Condition to check "Raymond" brand
public  GiftVoucher5000()
{
if (this.GetShirt().Brand != BrandName)
Console.WriteLine("Alert !!!");
else
Console.WriteLine("Success !!!");
}
//Abstract method which should be implemented by sub class method.
//Else it will throw an error.
public abstract Shirt GetShirt();
}
public class Cousin_Abs : GiftVoucher5000
{
//Overriding the abstract method. It will throw an error if it is not overrided.
public override Shirt GetShirt()
{
//Passing other brand should display "Alert" message
return new Shirt("Raymond", "Black");
}
}
public class Brother_Abs : GiftVoucher5000
{
//Overriding the abstract method
public override Shirt GetShirt()
{
//Passing Raymond brand should display "Success" message
return new Shirt("Tommy Hilfiger", "Blue");
}
}
static void Main(string[] args)
{
Cousin_Abs Ram = new Cousin_Abs();
Console.WriteLine("Your cousin bought {0} : {1} shirt !!!", Ram.GetShirt().Brand, Ram.GetShirt().Color);
Brother_Abs Sri = new Brother_Abs();
Console.WriteLine("Your brother bought {0} : {1} shirt !!!", Sri.GetShirt().Brand, Sri.GetShirt().Color);
Console.ReadLine();
}

Your cousin accepts to have Raymond brand but your brother wants only Tommy Hilfiger. In this case, Abstract class constructor will show "Alert" message based on the selected brand. The output will be
Success !!! Your cousin bought Raymond : Black shirt !!! Alert !!! Your brother bought Tommy Hilfiger : Blue shirt !!!
So to summarize everything,

Interface

  • Interface is not a class and defined with a word "Interface". It provides only blue print and will not have any implementation for any method.
  • All the methods in Interface should be implemented in subclass.
  • Any class can inherit multiple interfaces.
  • Only "Public" access modifier is allowed

Abstract Class

  • Abstract class provides certain standards or hierarchy and forces the subclasses to follow them. If needed, Abstract classes provides default implementation for certain methods and subclasses can use as it is.
  • No need to implement all the methods in abstract classes. Only abstract methods should be implemented.
  • Abstract class cannot be instantiated. That means you cannot create object for it and only can be inherited. Any class can inherit only one Abstract class.
  • Different access modifiers are allowed.
Comments