Contact Form

Name

Email *

Message *

Polymorphism

No comments

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

No comments :

Post a Comment