The definition of polymorphism is when different types of objects use the method or properties with the same name. Therefore, the programmer doesn't need to know the exact type of an object while coding the application. The precise behavior of a method or property is determined at run-time.
The single sentence definition of polymorphism is"The ability to take more than one form.".<br><br>__________________________________________________________<br><a href="http://www.imtpconsultants.com/">MBA consultants in India</a><br>
<P>This is a nice and simple <A href="http://www.questpond.com">OOP interview question</A>. As the name says POLY (Many) MORPHISM (Change). Polymorphism is the ability of the object to behave differently under different conditions. For instance a simple user object can behave as a admin user or as a simple user depending on conditions.</P>
<P>There are two ways of doing polymorphism dynamic and static. Static polymoprhism is achieved by using method overloading and dynamic polymorphism is acheived by using method overloading. Please note do confuse with overloading and overriding while answering this question. Many .NET developers make a niceversa statement leading to lot of confusion.</P>
<P>See my 50 .<A href="http://www.questpond.com">NET and ASP.NET Interview questions</A></P>
Polymorphism: It's derived from a Greek word, where poly means many morph means Faces/Behaviors.<div>Same function/operator will show different behaviours when passed different types of values or different number of values.</div><div>Types of polymorphism:There are 2 types:-</div><div>1. Static polymorphism/Early Binding/Compile time polymorphism.</div><div>2. Dynamic polymorphism/Late Binding/Run time polymorphism.</div><div><br></div><div>Static polymorphism is achieved using i) Function overloading ii) Operator overloading</div><div><br></div><div>Dynamic polymorphism is achieved using i) Function overloading<br><div><br></div></div>
polymorphism is popularly called as one interface,multiple functions.<div>The OOP allows object to respond differently for the same message.</div><div>It is an entity tat exists in different form simultaneously.</div><div><br></div><div>Static polymorphism</div><div>->function overloading</div><div>->operator overloading</div><div>->constructor overloading</div><div> 1>instance constructor</div><div> 2>static constructor</div><div><br></div><div>dynamic polymorphism</div><div> 1)abstract class</div><div> 2)virtual function</div><div> 3)delegates</div><div><br></div><div><br></div><div>-></div>
Jun 04, 2007
<td id="ctl00_ContentPlaceHolder1_GridViewAnswers_ctl07_td2" class="NormalText" width="500">
<p>In oops world the polymorphisam is to ability inherits methods and properties of baseclass into derived class.</p>
<p>Example of polymorphisam is </p>
<p>public class Addres_Details</p>
<p>//here comes default construtor</p>
<p>//Now I am defines one method </p>
<p> public virtual void add_nam</p></td></tr></tbody></table>
<PRE>1. function overloading,
2. Runtime polymorphism --Using Interface reference,
interface name I ,abstract Method();
implimenting classes A,B
objA= new A();
objB= new B();
Polymorphism means <br>Poly=many<br>morphism=form<br> Polymorphism is achieve by using function overloading.<br><br>
Polymorphism means ability to take more than one form.<br><br>VB.net and C# achieve polymorphism through inheritance.<br>When class is inheriting by generalized class in to a specialized class,it includes the behavior of the generalized class. But if we need some different kind of implementation rather than which provides by the top class we can implement it with our specialized class without making any changes to the name.<br> <br>Polymorphism comes in to action with dynamic binding, so it's important to identify which object to have which behavior. (VB.net and C# both are default to dynamic binding)<br><br>
Polymorphism refers to the ability to assume different forms. In OOP, it indicates a language’s ability to handle objects differently based on their runtime type. When objects communicate with one another, we say that they send and receive messages. The advantage of polymorphism is that the sender of a message doesn’t need to know which class the receiver is a member of. It can be any arbitrary class. The sending object only needs to be aware that the receiving object can perform a particular behavior.
A classic example of polymorphism can be demonstrated with geometric shapes. Suppose we have a Triangle, a Square, and a Circle. Each class is a Shape and each has a method named Draw that is responsible for rendering the Shape to the screen. With polymorphism, you can write a method that takes a Shape object or an array of Shape objects as a parameter (as opposed to a specific kind of Shape). We can pass Triangles, Circles, and Squares to these methods without any problems, because referring to a class through its parent is perfectly legal. In this instance, the receiver is only aware that it is getting a Shape that has a method named Draw, but it is ignorant of the specific kind of Shape. If the Shape were a Triangle, then Triangle’s version of Draw would be called. If it were a Square, then Square’s version would be called, and so on.
In oops world the polymorphisam is to ability inherits methods and properties of baseclass into derived class.
Example of polymorphisam is
public class Addres_Details
//here comes default construtor
//Now I am defines one method
public virtual void add_name
//Now i am defining new class and inherits the Address_details class
public class New_address_Details:Address_Details
public override void add_name()
console.writeln("This is the Example of the Polymorphisam");
Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details.
Polymorphism Overview : -When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class. To change the data and behavior of a base class, you have two choices: you can replace the base member with a new derived member, or you can override a virtual base member.
Replacing a member of a base class with a new derived member requires the new keyword. If a base class defines a method, field, or property, the new keyword is used to create a new definition of that method, field, or property on a derived class. The new keyword is placed before the return type of a class member that is being replaced.When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class.
Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language.
polymorpihsm ->one name, many form.
have 2 types : 1. Compile time, 2 Run time
1 compile time: also called overloadinghave 3 types. 1. Constructor overloading(i.e.,with same constructor name and different no of arguments or different datat types or both ) 2 Function overloading(i.e.,with same function name and different no of arguments or different datat types or both)
3. operator overloading: exaample : String s = "James";
s = s + "Bond";
Runtime poly: is achieved by overridding parent class method:
This is called Dynamic method dispatch.
Multiple use of single form is known as poly...
If I remember correctly, it is something like when you have the same method names but different implementations.
For example in the base class there is a method called PrintMessage().In the derived calss there is the same method name.
The way that this can be achived without errors is to have Virtual in the base class and override in the derived class for the method names.
I think this should give you a basic understanding of polymosrphism. I am not too sure though.
© 2016 C# Corner. All contents are copyright of their authors.
C# Corner. All contents are copyright of their authors.