Abstract Class Vs Interface

This is the most widely asked question in the interviews to the freshers as well as an experienced professional.

This is the most widely asked question in the interviews. A fresher or an experienced professional, either way, you would come across this question.

Difference between these two always coincides and the interviewer stresses particularly on this question to understand how good you are at understanding the concepts and implementing them.

We all know the theoretical part of what an Abstract class is and what an Interface is.

Why Abstract Class?

Because it has the implementation, without which it would act same as Interface.

Why Interface?

Because it has functionality that a class or struct can implement.

I've come with an example to explain in what scenarios the abstract class and interface work.

Example

  1. public class Doctors {  
  2.     public string Diagnose() {  
  3.         return "I have Diagnosed a disease";  
  4.     }  
  5.     public string Treatment() {  
  6.         return "I have treated this disease";  
  7.     }  
  8.     public string Medication() {  
  9.         return "I have prescribed medications";  
  10.     }  
  11. }  

Consider we have a separate class for each Doctor.

  1. public class Physician: Doctors {  
  2.     // Will have all the features of Doctors class  
  3. }  
  4. public class Orthopedician: Doctors {  
  5.     // Will have all the features of Doctors class and Doctor need to inherit surgery feature which is not in Doctors  
  6. }  
  7. public class Neurologist: Doctors {  
  8.     // Will have all the features of Doctors class and Doctor need to inherit surgery feature which is not in Doctors  
  9. }  

We need a method for inheriting the Surgery feature for Orthopedician and Neurologist. Though both are specialized surgeons, their operation process might be different.

So, we create an abstract class in which the method will be declared as Abstract and it should be implemented in child classes.

  1. public abstract class Surgeon {  
  2.     public abstract string Surgery();  
  3. }  

Now, we are trying to inherit from this abstract class and the kind of Surgery is implemented by Orthopedician and Neurologist.

  1. public class Orthopedician: Doctors, Surgeon {  
  2.     public override string Surgery() {  
  3.         return "I have done surgery for joints ";  
  4.     }  
  5. }  
  6. public class Neurologist: Doctors, Surgeon {  
  7.     public override string Surgery() {  
  8.         return "I have done surgery for the brain";  
  9.     }  
  10. }  

In the above two classes, Orthopedician and Neurologist are trying to inherit from Doctors and Surgeon class, which can't be done because an abstract class cannot support multiple inheritances.

Here. we need to use Interface to achieve the above concept.

  1. interface ISurgeon  
  2. {  
  3.    string GetSurgery();  
  4. }  

The implementation is given below.

  1. public class Orthopedician: Doctors, ISurgeon {  
  2.     public string GetSurgery() {  
  3.         return "I have done surgery for joints";  
  4.     }  
  5. }  
  6. public class Neurologist: Doctors, ISurgeon {  
  7.     public string GetSurgery() {  
  8.         return "I have done surgery for brain";  
  9.     }  
  10. }