Preparing .NET Interview - Part Six (OOPs)

I am here to continue my series related to .NET interview preparation. Today we will discuss the questions related to Object-Oriented Programming and present the answers in an easy way.
 
So let’s take the questions one by one:
  1. What is an abstraction in programming? Also, provide real world examples.
     
    Answer: Abstraction is the idea of showing only required information and hiding the complex implementation details from the end-user.
     
    For example in .NET Library, the methods like WriteLine and ToString are the example of abstraction as these methods are only exposing their name, input parameters and return types and not the actual definition or implementation.
     
    Maps API like Google or Bing is also an example of abstraction as they only expect country, city, and area names to return the results without exposing a whole lot of logic to the end-user.
     
    In the real-world, several examples can be put as follows.
     
    • Cell Phone: Presents the feature's icon on the home screen and hides their complex implementations.
    • Car: Presents the easy way to drive and hides the complex functioning of an engine.
    • Laptop: Exposes the easy way to operate without worry about its internal architecture and parts.
    • Electric fitting: Shows only the switches to operate and hides the messy wiring behind the wall.
     
  2. What is Virtual Method in C#? Can we declare them private?
     
    Answer:
     
    Virtual keyword is used when we want any method to be overridden in a child or extended classes. If any method is declared as virtual then it has to be overridden in child classes or the method should be declared as new.
     
    Virtual methods can’t be declared as private as they are meant to be overridden by child classes. Moreover, we get a compile-time error when declaring them as private as “virtual or abstract members cannot be private”.
     
    Example:
      1. class A {  
      2.     public virtual void Show() {  
      3.         Console.WriteLine("Called from base class");  
      4.     }  
      5. }  
      6. class B: A {  
      7.     public override void Show() {  
      8.         Console.WriteLine("Called from extended class");  
      9.     }  
      10. }  
      11.   
      12. class Program {  
      13.     static void Main(string[] args) {  
      14.         Console.Title = "OPPs questions demo";  
      15.   
      16.         A a1 = new A();  
      17.         a1.Show(); //Calls the base class method as instance of A is assigned.    
      18.         A a2 = new B();  
      19.         a2.Show(); //Calls the extended class method as instance of B is assigned.    
      20.         B b1 = new B();  
      21.         b1.Show(); //Calls the extended class method as instance of B is assigned.    
      22.     }  

        Output:
         
        5
         
      1. What is a new keyword in overriding? When to use it?
         
        Answer: new keyword is used to hide the base method with the same name.
         
        In a virtual/override scenario, if we have a virtual method in the base class, it has to be overridden in the extended class. If we don’t want to override then either we need to change the method name or have the same method with a new keyword.
         
        Example:
          1. class A {  
          2.     public virtual void Show() {  
          3.         Console.WriteLine("Called from base class");  
          4.     }  
          5. }  
          6. class B: A {  
          7.     public new void Show() {  
          8.         Console.WriteLine("Called from extended class");  
          9.     }  
          10. }  
          11.   
          12. class Program {  
          13.     static void Main(string[] args) {  
          14.         Console.Title = "OPPs questions demo";  
          15.   
          16.         A a1 = new A();  
          17.         a1.Show(); //Calls the base class method as instance of A is assigned.    
          18.         A a2 = new B();  
          19.         a2.Show(); //Calls the base class method as class B doesn’t have the overridden version of Show method.    
          20.         B b1 = new B();  
          21.         b1.Show(); //Calls the extended class method as instance of B is assigned irrespective of override and new.    
          22.     }  

            Output:
             
            6
             
          1. What are sealed objects and why we need them?
             
            Answer
            : Sealed objects by name means that they are kind of secure.
             
            • Sealed classes can’t be inherited.
            • Sealed methods can’t be used in a child or extended classes.
            • Sealed keyword can only be applied to instance members (methods, properties, events & indexes) and not to static once.
             
          2. How can we set the class to be inherited, but prevent the method from being over-ridden?
             
            Answer:  If the method is not virtual, it can’t be overridden by default.
             
            If the method is already overridden in the child class, the sealed keyword can be applied along with an override to avoid further overriding.
             
            Example:
              1. public class BaseClass {  
              2.     public int Add(int a, int b) {  
              3.         return (a + b);  
              4.     }  
              5.     public virtual float CalculateDiscount(float premium, float regularDiscount, float saleDiscount) {  
              6.         return (premium * (regularDiscount + saleDiscount)) / 100;  
              7.     }  
              8. }  
              9. class ChildClass1: BaseClass {  
              10.     //Add can't be overridden because it's not declared as virtual    
              11.     public sealed override float CalculateDiscount(float premium, float regularDiscount, float saleDiscount) {  
              12.         return (premium * (regularDiscount + saleDiscount)) / 100;  
              13.     }  
              14. }  
              15.   
              16. class ChildClass2: ChildClass1 {  
              17.     //CalculateDiscount can't be overridden because it's declared as sealed in ChildClass1    

              1. How can we achieve polymorphism without overloading and overriding?
                 
                Answer: Without overloading and overriding, we can achieve polymorphism partially by following ways.
                 
                • Generic types (classes/methods)
                • params keyword
                 
              2.  What is a Partial class and in which scenario it should be used? 
                 
                Answer:
                 
                A partial class is a class that has members split across files. Partial class should have the same accessibility across files. It can be useful in the following ways.
                 
                • When we want to extend the existing class with more members without changing the name.
                • When more than one developer is working on the same class.
                 
                Example:
                1. //File: Employee.cs  
                2. public partial class Employee  
                3. {  
                4.      public string Name { get; set; }  
                5.      public string City { get; set; }  
                6. }  
                7.   
                8. //File: EmployeeDetail.cs  
                9. public partial class Employee  
                10. {  
                11.      public string State { get; set; }  
                12.      public decimal Salary { get; set; }  
                13. }   
              Hope you have liked the article. Look forward to your comments/suggestions.
               
              Read more articles on .NET Core