Part - 4 : Oops Interview Question's Answer


31. Can you declare the override method static while the original method is non-static?

No

32. What is mean by Sealed Class?

Class which cannot be inherited is called as sealed class. If we need to prevent a class from being inherited, use "Sealed" keyword. But sealed class can inherited from other classes.
Example :
public class MyBaseClass
 { 
  public void Display()
   {
     Console.WriteLine("Base class");
   }
 }

 //Compile Success: This class cannot be inherited
 public sealed class MySealedClass:MyBaseClass 
  {
   public void Display()
    {
      base.Display();
      Console.WriteLine("Sealed class");
    }
  }

 //Compilation Error: cannot derive from sealed type MySealedClass
 public class MyChildClass : MySealedClass
  { 
  }

33. Can you allow class to be inherited, but prevent the method from being over-ridden?

Yes, just leave the class public and make the method sealed.

34. Will sealed class allows inheritance, if not why?

Sealed means it is not inheritable

35. What are the advantages of Private constructor?

  1. Private constructor will prevent the user from creating the instance of the class which contains only static members.
  2. Private constructor are used for implementing the singleton pattern


36. While using inheritance, derived class construct will call base class constructor?

Yes, base class constructor will be called before child class constructor

37. Overloaded constructor will call default constructor internally?

No, overload constructor will not call default constructor

38. What is difference between Overrides and Overridable?

Overridable (VB.Net)/ virtual (C#) is used in parent class to indicate that a method can be overridden. Overrides(VB.Net)/ override(C#) is used in the child class to indicate that you are overriding a method.

39. What is Method overloading?

Method overloading occurs when a class contains two methods with the same name, but different signatures.

40. What is operator overloading?

Operator overloading is used to provide a custom functionality to existing operators. For Example +,-,* and / operators are used for mathematical functionality. But we can overload these operators to perform custom operation on classes or structure.
Example :
To concatenate the two strings we have to use Concat method
 Dim str1, str2, str3 As String
 str1 = "Hello"
 str2 = "world"
 str3 = String.Concat(str1, str2)
But .Net provides in build operator overloading for string we can use "+" operator for concatenating the string value str3=str1+str2 Similarly we can also implement operator overloading for classes or structure Employee3= Employee1 + Employee2

Post a Comment

0 Comments