When to Use Interface And When To Use Abstract Class - Part Three

I hope after reading my previous articles now readers can easily make out the differences between Interface and Abstract Class.

Still I recommend that you please go through my previous articles:

Now let’s start with the most awaited debate: when to use interface and when to use abstract class. Throughout this article I will use a car as an example. It will be easy for readers to correlate the things and all are aware of the basic features of a car.

Look at the few points below for better  understanding,
  1. Abstract class provides user flexibility. Where a situation is uncommon, use non-abstract methods, and where a situation is common use abstract methods. In case you are aware of WCF, think about contracts, and use interface when you need a contract on some specific behavior.

  2. Let's say I have to design a system for a sedan for Hyundai and Maruti. Now Hyundai uses its own engine and Maruti uses engines provided by Ford. Both engines work differently (internally). And the way they get assembled is also different. So let's say we will define an abstract class called “Engine” and I have defined four methods in that; i.e. ,Displacement, Power, Torque. and Cylinder. Now let's say Hyundai wants to add a “K“ series with another factor called “X.”

    Now this “X” factor only can be implemented in Hyundai engines, not in other cars.

    Problem????

    Solution: We can define one more abstract class, but again we have an issue. Multiple inheritances are not supported in C#. Again ---  Problem??

    Genuine Solution: Use Interface

    Look at the sample below,
    1. public abstract class Engine  
    2.     {  
    3.   
    4.         public abstract void Displacemen();  
    5.         public abstract void Power();  
    6.         public abstract void Torque();  
    7.         public abstract void Cylinder();  
    8.   
    9.     }  
    10.   
    11. class MarutiEngineImplementation : Engine  
    12.     {  
    13.         public override void Displacemen()  
    14.         {  
    15.             throw new NotImplementedException();  
    16.         }  
    17.   
    18.         public override void Power()  
    19.         {  
    20.             throw new NotImplementedException();  
    21.         }  
    22.   
    23.         public override void Torque()  
    24.         {  
    25.             throw new NotImplementedException();  
    26.         }  
    27.   
    28.         public override void Cylinder()  
    29.         {  
    30.             throw new NotImplementedException();  
    31.         }  
    32.     }  
    33.   
    34. class HyundaiEngineImplementation:Engine  
    35.     {  
    36.         public override void Displacemen()  
    37.         {  
    38.             throw new NotImplementedException();  
    39.         }  
    40.   
    41.         public override void Power()  
    42.         {  
    43.             throw new NotImplementedException();  
    44.         }  
    45.   
    46.         public override void Torque()  
    47.         {  
    48.             throw new NotImplementedException();  
    49.         }  
    50.   
    51.         public override void Cylinder()  
    52.         {  
    53.             throw new NotImplementedException();  
    54.         }  
    55.     }  

    Now when I try to implement the “X” factor in Hyundai, I need to implement the same in Maruti as well, which is not at all needed. For such an example we need to use Interface.

  3. Abstract class can be extended into the future, especially with non-abstract methods, but you cannot touch the interface as it may lead you into trouble. Let's say you want to write one new method in class where speed is fixed. with a  max up to 150Km/hr. So all the corresponding classes can use the same. But once you have defined the interface it’s preferable not to touch it. Rather, create a new one.

Conclusion

I hope the above example helps you in understanding where to use interface and where to use abstract class. Now I hope in interviews you will be able to crack this question easily.