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 dynamicallyb)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.
new method.It can be concluded that the method defined is independent of
base class method.
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)
public void method(out int a)
{
}
}
No.methods cannot be overloaded based on the ref and out parameter.
d)
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)
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)
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.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
//print A
}
}
class B:A
{
public override void method()
{
//print B
//print B
}
}class C:B
{
public new void method()
{
//Print C
//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
Question 9 a):
ReplyDeleteclass 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();
}
}
ans for que 9 would be for class c object, the new method outputs C on console.
ReplyDeleteobjB= new C()
ReplyDeleteobjB.method()
OutPut : C
Right. The author got it wrong. I literally just tried this code in VS and it outputs C.
DeleteB obj = new C();
Deleteobj.method(); // output- B
C cobj= new C();
cobj.methode(); // output - C
Hey Nice Post, very useful content
ReplyDeleteDotnet Interview Question and Answer
http://allittechnologies.blogspot.in/search/label/Interview%20Question%20and%20Answer
what is use of Polymorphism ???
ReplyDelete(use of Polymorphism in coding application)
with example...
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.
ReplyDeleteQ No 9. Output is C. method of class C is called.
ReplyDeleteAllitTechnolgies Interview Question and Answers :
ReplyDeleteDotnet Interview Question and Answers
question 9
ReplyDeleteobjC= new C()
objC.method() //Output should be : C
Right ?
I've never seen this information be presented any simpler and so easy to understand. Thank you!
ReplyDeleteATTENTION: 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.
ReplyDeleteInvoking the method name directly like
objC.method()
will just call the now "hidden" function.
To access the new function:
((C)objC).method()
A Nice article and polymorphysim more advanteges.
ReplyDeletebest c sharp training institute in chennai
I've never seen this information be presented any simpler and so easy to understand. Thank you!
ReplyDeleteApp V Online Training
Sailpoint Online Training
Dotnet Online Training
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!
ReplyDeletepython training Course in chennai | python training in Bangalore | Python training institute in bangalore
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.
ReplyDeleteexcel advanced excel training in bangalore | Devops Training in Chennai
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteJava training in Bangalore | Java training in Kalyan nagar
Java training in Bangalore | Java training in Kalyan nagar
Java training in Bangalore | Java training in Jaya nagar
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.
ReplyDeleteangularjs Training in bangalore
angularjs Training in bangalore
angularjs online Training
angularjs Training in marathahalli
angularjs interview questions and answers
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.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Wonderful thanks for sharing an amazing idea. keep it...
ReplyDeleteLooking for Data Warehousing Training in Bangalore, learn from Softgen Infotech provide Data Warehousing Training on online training and classroom training. Join today!
very nice... !
ReplyDeleteinplant training in chennai
inplant training in chennai
inplant training in chennai for it
brunei darussalam web hosting
costa rica web hosting
costa rica web hosting
hong kong web hosting
jordan web hosting
turkey web hosting
gibraltar web hosting
very nice post...
ReplyDeleteinplant training in chennai
inplant training in chennai
inplant training in chennai for it
Australia hosting
mexico web hosting
moldova web hosting
albania web hosting
andorra hosting
australia web hosting
denmark web hosting
Some spelling mistake is there. Please correct that. Otherwise article is nice
ReplyDeleteThanks