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





24 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
  3. objB= new C()
    objB.method()

    OutPut : C

    ReplyDelete
    Replies
    1. Right. The author got it wrong. I literally just tried this code in VS and it outputs C.

      Delete
    2. B obj = new C();
      obj.method(); // output- B

      C cobj= new C();
      cobj.methode(); // output - C

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

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

    ReplyDelete
  6. 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
  7. Q No 9. Output is C. method of class C is called.

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

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

    ReplyDelete
  10. ATTENTION: The output in #9 is correct. The "new" keyword just hides the base class's function, it does not make the derived class's function override it. If you want to access the derived class's version, use typecasting.

    Invoking the method name directly like

    objC.method()

    will just call the now "hidden" function.
    To access the new function:

    ((C)objC).method()

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

    App V Online Training
    Sailpoint Online Training
    Dotnet Online Training

    ReplyDelete
  12. Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
    python training Course in chennai | python training in Bangalore | Python training institute in bangalore

    ReplyDelete
  13. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    excel advanced excel training in bangalore | Devops Training in Chennai

    ReplyDelete
  14. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete
  15. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  16. Wonderful thanks for sharing an amazing idea. keep it...

    Looking for Data Warehousing Training in Bangalore, learn from Softgen Infotech provide Data Warehousing Training on online training and classroom training. Join today!

    ReplyDelete
  17. Some spelling mistake is there. Please correct that. Otherwise article is nice

    Thanks

    ReplyDelete