Sunday 13 January 2013

Interview Questions on oops

1. How to prevent a class from being inherited?

In order to prevent a class in C# from being inherited, the keyword sealed is used. Thus a sealed class may not serve as a base class of any other class. It is also obvious that a sealed class cannot be an abstract class. Code below...

//C# Examplesealed class ClassA{    public int x;    public int y;}


No class can inherit from ClassA defined above. Instances of ClassA may be created and its members may then be accessed, but nothing like the code below is possible...

class DerivedClass : ClassA { } // Error

2. Implementation inheritance and interface inheritance?

  • When a class is derived from another class in such a way that it will inherit all its members to its corresponding derived class, then it is implementation inheritance. 
  • When a class inherits only the signatures of the functions from its corresponding base class, then it is interface inheritance.

  • 3. How to: Explicitly Implement Interface Members with Inheritance
      Explicit interface implementation also allows the programmer to implement two interfaces that have the same member names and give each interface member a separate implementation. This example displays the dimensions of a box in both metric and English units. The Box class implements two interfaces IEnglishDimensions and IMetricDimensions, which represent the different measurement systems. Both interfaces have identical member names, Length and Width

    // Declare the English units interface:
    interface IEnglishDimensions
    {
        float Length();
        float Width();
    }

    // Declare the metric units interface:
    interface IMetricDimensions
    {
        float Length();
        float Width();
    }

    // Declare the Box class that implements the two interfaces:
    // IEnglishDimensions and IMetricDimensions:
    class Box : IEnglishDimensions, IMetricDimensions
    {
        float lengthInches;
        float widthInches;

        public Box(float length, float width)
        {
            lengthInches = length;
            widthInches = width;
        }

        // Explicitly implement the members of IEnglishDimensions:
        float IEnglishDimensions.Length()
        {
            return lengthInches;
        }

        float IEnglishDimensions.Width()
        {
            return widthInches;
        }

        // Explicitly implement the members of IMetricDimensions:
        float IMetricDimensions.Length()
        {
            return lengthInches * 2.54f;
        }

        float IMetricDimensions.Width()
        {
            return widthInches * 2.54f;
        }

        static void Main()
        {
            // Declare a class instance box1:
            Box box1 = new Box(30.0f, 20.0f);

            // Declare an instance of the English units interface:
            IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

            // Declare an instance of the metric units interface:
            IMetricDimensions mDimensions = (IMetricDimensions)box1;

            // Print dimensions in English units:
            System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
            System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

            // Print dimensions in metric units:
            System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
            System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
        }
    }



    4. Access the base class method with derived class objects

    First cast the derived class object to base class type and if you call method it invokes base class method. Keep in mind it works only when derived class method is shadowed


    public class BaseClass{    public void Method1()    {        string a = "Base method";    }}
    public class DerivedClass : BaseClass{    public new void Method1()    {        string a = "Derived Method";    }}
    public class TestApp{    public static void main()    {        DerivedClass derivedObj = new DerivedClass();        BaseClass obj2 = (BaseClass)derivedObj;        obj2.Method1();  // invokes Baseclass method
        }}


    5.Difference between ‘Shadowing’ and ‘Overriding’ (Shadowing vs Overriding) – C#


    Shadowing : Creating an entirely new method with the same signature as one in a base class.
    Overriding: Redefining an existing method on a base class.
    • Both are used when a derived class inherits from a base class
    Both redefine one or more of the base class elements in the derived class

    class A
     {
     public int M1()
     {
     return 1;
     }
     public virtual int M2()
     {
     return 1;
     }
     }
    class B : A
     {
     public new int M1()
     {
     return 2;
     }
     public override int M2()
     {
     return 2;
     }
     }
    class Program
     {
     static void Main(string[] args)
     {
     B b = new B();
     A a = (A)b;
     Console.WriteLine(a.M1()); //Base Method
     Console.WriteLine(a.M2()); //Override Method
     Console.WriteLine(b.M1());
     Console.WriteLine(b.M2());
     Console.Read();
     }
     }
    6. Shadowing vs Overriding

    It is easy to confuse shadowing with overriding. Both are used when a derived class inherits from a base class, and both redefine one declared element with another. But there are significant differences between the two.

    You normally use overriding in the following cases:
    • You are defining polymorphic derived classes.
    • You want the safety of having the compiler enforce the identical element type and calling sequence.
    You normally use shadowing in the following cases:
    • You anticipate that your base class might be modified and define an element using the same name as yours.
    • You want the freedom of changing the element type or calling sequence.


    suppose you have this classes:

    class A {
       public int Foo(){ return 5;}
       public virtual int Bar(){return 5;}
    }
    class B : A{
       public new int Foo() { return 1;}
       public override int Bar() {return 1;}
    }

    then when you call this:

    A clA = new A();
    B clB = new B();

    Console.WriteLine(clA.Foo()); // output 5
    Console.WriteLine(clA.Bar()); // output 5
    Console.WriteLine(clB.Foo()); // output 1
    Console.WriteLine(clB.Bar()); // output 1

    //now let's cast B to an A class
    Console.WriteLine(((A)clB).Foo()); // output 5 <<<--
    Console.WriteLine(((A)clB).Bar()); // output 1

    Suppose you have a base class and you use the base class in all your code instead of the inherited classes, and you use shadow, it will return the values the base class returns instead of following the ineritance tree of the real type of the object.
    7. Common c# Interfaces
    System.Configuration.IApplicationSettingsProvider System.Linq.IQueryable
    System.Configuration.IConfigurationSectionHandler System.Net.ICredentials
    System.Data.IDataAdapter System.Web.IHttpHandler
    System.Data.IDataParameter System.Web.IHttpModule
    System.Data.IDataReader System.Web.SessionState.IHttpSessionState
    System.Data.IDbCommand System.Web.SessionState.IReadOnlySessionState
    System.Data.IDbConnection System.Web.SessionState.IRequiresSessionState
    System.Data.IDbDataAdapter System.Xml.Serialization.IXmlSerializable
    System.Security.Principal.IPrincipal System.Security.Principal.IIdentity