Preparing .NET Interview - Part Six (OOPs)

I am here to continuemy 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 takethe questions one by one:
  1. What is abstraction in programming? Also provide real world examples.

    Answer: Abstraction is an idea of showing only required information and hiding the complex implementation details from 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 are also the example of abstraction as they only expect country, city and area names to return the results without exposing whole lot of logic to the end user.

    In real world, several examples can be put as following.

    • Cell Phone: Presents the features icon in home screen and hides their complex implementations.
    • Car: Presents the easy way to drive and hides the complex functioning of 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 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 method can’t be declared as private as they are meant to be overridden by child classes. Moreover, we get compile time error when declaring them as private as “virtual or abstract members cannot be private”.

    Example:
    1. class A  
    2. {  
    3. public virtual void Show()  
    4. {  
    5. Console.WriteLine("Called from base class");  
    6. }  
    7. }  
    8. class B: A  
    9. {  
    10. public override void Show()  
    11. {  
    12. Console.WriteLine("Called from extended class");  
    13. }  
    14. }  
    15.   
    16. class Program  
    17. {  
    18. static void Main(string[] args)  
    19. {  
    20. Console.Title = "OPPs questions demo";  
    21.   
    22. A a1 = new A();  
    23. a1.Show(); //Calls the base class method as instance of A is assigned.  
    24. A a2 = new B();  
    25. a2.Show(); //Calls the extended class method as instance of B is assigned.  
    26. B b1 = new B();  
    27. b1.Show(); //Calls the extended class method as instance of B is assigned.  
    28. }  
    29. }  
    Output:
     
    5


  3. What is 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 virtual method in base class, it has to be overridden in extended class. If we don’t want to override then either we need to change the method name or have the same method with new keyword.

    Example:
    1. class A  
    2. {  
    3. public virtual void Show()  
    4. {  
    5. Console.WriteLine("Called from base class");  
    6. }  
    7. }  
    8. class B: A  
    9. {  
    10. public new void Show()  
    11. {  
    12. Console.WriteLine("Called from extended class");  
    13. }  
    14. }  
    15.   
    16. class Program  
    17. {  
    18. static void Main(string[] args)  
    19. {  
    20. Console.Title = "OPPs questions demo";  
    21.   
    22. A a1 = new A();  
    23. a1.Show(); //Calls the base class method as instance of A is assigned.  
    24. A a2 = new B();  
    25. a2.Show(); //Calls the base class method as class B doesn’t have the overridden version of Show method.  
    26. B b1 = new B();  
    27. b1.Show(); //Calls the extended class method as instance of B is assigned irrespective of override and new.  
    28. }  
    29. }  
    Output:

    6


  4. 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 child or extended classes.
    • Sealed keyword can only be applied to instance members (methods, properties, events & indexes) and not to static once.

  5. How can we set class to be inherited, but prevent the method from being over-ridden?

    Answer:  If method is not virtual, it can’t be overridden by default.

    If method is already overridden in child class, sealed keyword can be applied along with override to avoid further overriding.

    Example:
    1. public class BaseClass  
    2. {  
    3. public int Add( int a , int b)  
    4. {  
    5. return (a + b);  
    6. }  
    7. public virtual float CalculateDiscount(float premium, float regularDiscount, float saleDiscount)  
    8. {  
    9. return (premium * (regularDiscount+ saleDiscount)) /100;  
    10. }  
    11. }  
    12. class ChildClass1: BaseClass  
    13. {  
    14. //Add can't be overridden because it's not declared as virtual  
    15. public sealed override float CalculateDiscount(float premium, float regularDiscount, float saleDiscount)  
    16. {  
    17. return (premium * (regularDiscount + saleDiscount)) / 100;  
    18. }  
    19. }  
    20.   
    21. class ChildClass2 : ChildClass1  
    22. {  
    23. //CalculateDiscount can't be overridden because it's declared as sealed in ChildClass1  
    24. }  
  6. 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

  7.  What is Partial class and in which scenario it should be used.  

    Answer
    :

    Partial class is the class which have members split across files. Partial class should have the same accessibility across file. It can be useful in following ways.

    • When we want to extend the existing class with more members without changing the name.
    • When more than one developers are 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 for your comments/suggestions.
 
Read more articles on .NET Core

Up Next
    Ebook Download
    View all
    Learn
    View all