Polymorphism

1..What is Polymorphism.explain diff types.
Ability to take more than one form is called poymorphism.Diff types are-
Compile time/Design time polymorphism
this is also called method overloading.The method will have same name but diff parameters.
Run time polymorphism
this is also called overriding.its achieved by virtual and override keywords 

2.What is method overloading ?
Having different methods with same name but different parameters inn a single class is called method overloading.methods can be overloaded based on following-
a)diff no of paramteres.
              public void method(int a)
              public void method(int a,int b)
b)diff types of parameters.
              public void method(int a)
              public void method(float a)
c)diff order of parameters.
             public void method(int a,float b)
             public void method(float b,int a)

3.When should we use method overloading?
When you need couple of methods to take different parameters but do the same thing.
eg-Draw(circle c),Draw(triangle t)... the basic function is drawing but they draw diff structures. in CLR console.writeline() does the same thing.

4.What is method overriding?
Its a feature which is used in inheritance chain and it provides its own implementation to an already existing method in base class. its achived by using virtual and override keyword.
class A
{
     public virtual void method()
     {
     }
}
class B:A
{
    public override void method()
     {
     }
}
The appropriate methods are invoked at runtime when proper references are allocated.

5.Advantages of polymorphism.
a)invoking child class functions dynamically
b)maintenance of code becomes easy.

6.Whats the diff bt new and override keyword in inheritance chain ?

new keyword completely hides the base class implementation and creates a
new method.It can be concluded that the method defined is independent of
base class method.

override keyword overrides the base class implementation. it helps in existence of different diff versions of method and appropriate version is called dynamically.Objects of derived class will cal this method instead of base class method.

7.Will the foll code compile ? why ?
a)
class A
{
     public void method()
     {
     }
}
class B:A
{
     public override void method()
     {
     }
}
No.Because you can override the base class method only if its marked as virtual

b)
class A
{
     public void method()
     {
     }
}
class B:A
{
     public new void method()
     {
     }
}
Yes.Because new keyword defines its own implementation and its not related to base class method in any way.

c)
class A
{
     public void method(ref int a)
     {
     }
     public void method(out int a)
     {
     }
}
No.methods cannot be overloaded based on the ref and out parameter.

d)
class A
{
     public virtual void method()
     {
     }
}
class B:A
{
     public void method()
     {
     }
}
class C:B
{
     public override void method()
     {
     }
}
No.Class C is overriding method in its base class B. it can do so only if method in base class is marked as virtual.

e)
class A
{
     public virtual void method()
     {
     }
}
class B:A
{
     public new void method()
     {
     }
}
class C:B
{
     public override void method()
     {
     }
}

No.Class C is overriding method in its base class B. it can do so only if method in base class is marked as virtual.

8.Whats the disadvantage of using virtual keyword?
a)Appropriate function calls are determined only at runtime.
b)since virtual keyword is used derived classes may ignore that base class implementations.

9.Whats the output of foll code
a)
class A
{
     public virtual void method()
     {
      //print A
     }
}
class B:A
{
     public override void method()
     {
      //print B
     }
}
class C:B
{
     public new void method()
     {
       //Print C
     }
}
A objA = new A();
objA .method()                              //Output:A

objB= new B()
objB.method()                               //Output:B
                          
objC= new C()
objC.method()                               //Output:B(bc of new keyword)


10.Why static methods cannot have virtual keyword ?
The idea of using virtual keyword is  to achieve polymorphism i.e calling appropriated functions dynamically at runtime, But static methods are attached to the class names and to invoke them you have to go through the classes. And they are decided at compile time hence there is no point in having virtual keyword to static methods.

11.Can properties be marked as virtual ?
Yes





15 comments:

  1. Question 9 a):
    class AA
    {
    public virtual void method()
    {
    Console.WriteLine("A");//print A
    }
    }
    class BB : AA
    {
    public override void method()
    {
    Console.WriteLine("B");//print B
    }
    }
    class CC : BB
    {
    public new void method()
    {
    Console.WriteLine("C");//Print C
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    //A[] tab = { new C(), new A(), new B() };
    //foreach (A a in tab)
    //{
    // B b = a as B;
    // if(b!=null)
    // b.Print();
    //}

    AA objA = new AA();
    objA .method(); //Output:A

    AA objAB = new BB();
    objAB.method(); //Output:B

    BB objB = new BB();
    objB.method(); //Output:B

    BB objBC = new CC();
    objBC.method(); //Output:B

    CC objC = new CC();
    objC.method(); //Output:C(bc of new keyword)

    Console.Read();

    }
    }

    ReplyDelete
  2. ans for que 9 would be for class c object, the new method outputs C on console.

    ReplyDelete
    Replies
    1. Tks very much for your post.

      Avoid surprises — interviews need preparation. Some questions come up time and time again — usually about you, your experience and the job itself. We've gathered together the most common questions so you can get your preparation off to a flying start.

      You also find all interview questions at link at the end of this post.

      Source: Download Ebook: Ultimate Guide To Job Interview Questions Answers:

      Best rgs

      Delete
  3. objB= new C()
    objB.method()

    OutPut : C

    ReplyDelete
  4. Tks very much for your post.

    Avoid surprises — interviews need preparation. Some questions come up time and time again — usually about you, your experience and the job itself. We've gathered together the most common questions so you can get your preparation off to a flying start.

    You also find all interview questions at link at the end of this post.

    Source: Download Ebook: Ultimate Guide To Job Interview Questions Answers:

    Best rgs

    ReplyDelete
  5. Hey Nice Post, very useful content
    Dotnet Interview Question and Answer
    http://allittechnologies.blogspot.in/search/label/Interview%20Question%20and%20Answer

    ReplyDelete
  6. what is use of Polymorphism ???
    (use of Polymorphism in coding application)
    with example...

    ReplyDelete
  7. Please update your OUTPUT for question 9, as it is creating a lot of confusions. I had to go through "https://msdn.microsoft.com/en-us/library/ms173153.aspx" to clear my concepts again.

    ReplyDelete
  8. Q No 9. Output is C. method of class C is called.

    ReplyDelete
  9. question 9
    objC= new C()
    objC.method() //Output should be : C
    Right ?

    ReplyDelete
  10. I've never seen this information be presented any simpler and so easy to understand. Thank you!

    ReplyDelete