Preparing for .NET Interviews - Part Seven (Abstract Class & Interface)

I am here to continue the series related to .NET interview preparation. Today we will discuss the questions related to Abstract Class & Interface and present the answers in an easy way. 

So let’s take questions one by one.
  1. What is an abstract class in .NET and when it is used?


    Answer:

    Abstract class is a high-level or incomplete class as one or more methods are only declared and left to be implemented by derived classes. These just declared or unfinished methods need to be marked as abstract. Abstract class and members need to follow few rules as follows.

    • Instance of an abstract class can’t be created.
    • Abstract class can’t be marked as sealed as it will stop it from implementing.
    • If any class has at least one abstract method, then the class needs to be declared as abstract.
    • Abstract methods when implemented in derived classes, need to mark as an override.

    Example:
    1. abstract class BaseClass  
    2.     {  
    3.         public abstract float CalculateDiscount(float premium, float regularDiscount, float saleDiscount);   
    4.         public void Print()  
    5.         {  
    6.             Console.WriteLine("Hello Prakash");  
    7.         }   
    8.     }  
    9. class DerivedClass: BaseClass  
    10.     {          
    11.         public override float CalculateDiscount(float premium, float regularDiscount, float saleDiscount)  
    12.         {  
    13.             return (premium * (regularDiscount + saleDiscount)) / 100;  
    14.         }  
    15.     }  
    16.   
    17. public class Program  
    18.     {  
    19.         static void Main()  
    20.         {  
    21.             Console.Title = "Abstract class demo";  
    22.             DerivedClass dClass = new DerivedClass();              
    23.             Console.WriteLine("Discount is:{0}", dClass.CalculateDiscount(1000, 10, 2));  
    24.             dClass.Print();  
    Output:

    Output

  2. What is interface and what are the benefits of interface based design?


    Answer:

    An Interface can be defined as an operations contract in which classes that implement interface must implement all its members.

    Methods, Properties, events, and indexers can be the members of an Interface.

    Inside a namespace, an interface can be declared as public or internal however its members are by default abstract and public.

    Example:
    1. public interface IEmployee  
    2.     {          
    3.         int GetEmpId();  
    4.         string GetEmpName();  
    5.         string GetDeptName();  
    6.         void AssignNewProject(int projectId);  
    7.     }  
    8.   
    9. public class Employee : IEmployee  
    10.     {          
    11.         public int GetEmpId()  
    12.         {  
    13.             return 101;  
    14.         }  
    15.   
    16.         public string GetEmpName()  
    17.         {  
    18.             return "Prakash";  
    19.         }  
    20.   
    21.         //string GetDeptName() //Error as interface members are public by default.  
    22.         public string GetDeptName()  
    23.         {  
    24.             return "Technology";  
    25.         }  
    26.         public void AssignNewProject(int projectId)  
    27.         {  
    28.             //Call some service to update the project  
    29.             Console.WriteLine("New Project is Assigned successfully");  
    30.         }  
    31.     }  
    32.   
    33. public class Program  
    34.     {  
    35.         static void Main()  
    36.         {              
    37.             Console.Title = "Interface demo";  
    38.             IEmployee emp = new Employee();              
    39.             Console.WriteLine("Emp Id: {0}", emp.GetEmpId());  
    40.             Console.WriteLine("Emp Name: {0}", emp.GetEmpName());  
    41.             Console.WriteLine("Dept Name: {0}",emp.GetDeptName());  
    42.             emp.AssignNewProject(1);  
    43.        }  
    44.    }  
    Output:

    Output

    It is recommended to keep only the common set of methods in interfaces so that classes that implement them can provide the definition and don’t leave them as “Not-Implemented”.

    There are several benefits of interface-based design as following.

    • Interface offers object composition or “black box reuse” due to the client only knows the signatures of members and not their implementations.
    • Interface provides clear segregation of contract/declaration and implementation.
    • Interface enforces loosely coupled design that is easier to reuse, test, and extend.
    • Interface by behavior supports runtime polymorphism.
    • Passing interface reference in methods is faster than passing concrete objects.
    • Test Driven Development (TTD) becomes easy with interface-based design.

  3. What’s are the similarities and differences between abstract class and interface?


    Answer:

    Similarities:
    • Both abstract class and Interface are abstract type objects.
    • Abstract class and Interface both are not eligible for new instance creation instead their Implementor needs to do the same.
    • In general terms, the interface can also be said as a refined abstract class with only public abstract methods.

    Differences:

    • Abstract class is used when we want to keep a set of common methods to be available for all its derived classes along with some abstract methods whereas interface is used when we want all its members to be implemented by derived classes.
    • Class that implements abstract class needs to implement only its abstract members whereas class that implement interface needs to implement all its members.
    • Abstract members of abstract classes can be declared as protected, internal and public whereas all the interface members are by default public.

  4. What’s the difference between implicit and explicit interface implementation.


    Answer:

    Please go through my article as following to get the answer.
Hope you have liked the article. Look forward to your comments/suggestions.
 
Read more articles on .NET:


Similar Articles