Polymorphism means “many shapes”. Study the following example. Note that a football, while actually a “ball” may require a different Bounce() method than a Hardball.
using System; using System.Collections.Generic; using System.Text;
One of the benefits is writing less code. Note that every new derivative of Ball inherits the Bounce() method. So it’s possible you won’t have to rewrite that.
Tell us the kinds of things ("objects") you deal with on a regular basis, and we might be able to give you an example that's closer to home.
So olivetree is a type of a tree, it inherits all tree properties and has special ones that make it an olivetree.
Class pinetree inherits from class tree.
Now pinetree inherits all tree properties and has special ones that make it a pinetree.
Both derived classes, olivetree and pinetree, are trees. Both of these classes can be treated as a tree class. Now we can say the tree class can take on 'many forms'; it can be a pinetree or an olivetree.
The declaration part (Ball b) means that b is a variable of type "Ball". That means that the value of b is always either null or a reference to an instance of Ball or a type derived (possibly indirectly) from Ball.
The right hand side of the assignment creates a new Hardball instance, and then the assignment makes the value of b a reference to the newly created Hardball object.
The main advantage of polymorphism is that you can interact with a group of objects identically, but have different behaviour depending on the type of object. Let's say you have an abstract Window base class with various concrete derived classes such as ScrollableWindow, SplitterWindow, etc. How each window type minimizes itself is different. If you didn't have polymorphism, you would have to write code like this:
// Don't do this!!! Bad code if you don't use polymorphism
Since Minimize is not a virtual method (i.e. using polymorphism), if you called win.Minimize(), you would execute the base class implementation, which is likely not suitable. So you have to find out the exact derived class (using the switch) and explicitly execute the correct method through a variable of the appropriate derived type. Note that this code is incredibly fragile. If you forget a window type, things don't work properly. (Best case you throw an exception and notice the problem. Worst case things just fail mysteriously.) This situation is compounded when you start adding new window types to the system. You likely have this type of switch/case code throughout your codebase and missing one instance causes your app to break.
Enter polymorphism. You declare your Minimize method as virtual (i.e. using polymorphism) and you now have the following code.
public class WindowManager {
public void MinimizeAll() {
foreach(Window win in m_windows) {
win.Minimize();
}
}
}
Polymorphism takes care of calling the correct derived class method. If you add new window types, it just works. The WindowManager code and the derived Window class code need not even be compiled together. (i.e. When you write WindowManager, the SuperDuperFooBarWindow class hasn't even been written.) I hope that you can see that polymorphism allows for much cleaner, maintainable, and elegant code.
When you develop a library of business objects, you use polymorphism to give each object a common set of properties, this is achieved by defining a base class and deriving business classes from it.
can anyone explain polymorphism
hytechpro
i understood the code but can u explain what this line means
Ball b = new Hardball( 2.0, 0.85 );
Prasadi de Silva
Polymorphism means “many shapes”. Study the following example. Note that a football, while actually a “ball” may require a different Bounce() method than a Hardball.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1 {
class Ball {
double diameter;
double coeffOfRestitution;
public Ball(double diameter, double coefficient) {
this.diameter = diameter;
this.coeffOfRestitution = coefficient;
}
//==================================================
virtual public string IAmA() {
return "Ball";
}
//==================================================
public double BounceHeight(double velocity) {
return velocity * coeffOfRestitution;
}
}
class Hardball : Ball { // Hardball is a Ball
public Hardball(double diameter, double coefficient)
: base( diameter, coefficient ) {
}
//==================================================
override public string IAmA() {
return "Hardball";
}
}
class App {
static void Main() {
Ball b = new Hardball( 2.0, 0.85 );
Console.WriteLine(b.IAmA());
}
}
}
Roderick Prince
One of the benefits is writing less code. Note that every new derivative of Ball inherits the Bounce() method. So it’s possible you won’t have to rewrite that.
Tell us the kinds of things ("objects") you deal with on a regular basis, and we might be able to give you an example that's closer to home.
Denis Brasfield
Class tree
Class olivetree inherits from class tree.
So olivetree is a type of a tree, it inherits all tree properties and has special ones that make it an olivetree.
Class pinetree inherits from class tree.
Now pinetree inherits all tree properties and has special ones that make it a pinetree.
Both derived classes, olivetree and pinetree, are trees. Both of these classes can be treated as a tree class. Now we can say the tree class can take on 'many forms'; it can be a pinetree or an olivetree.
Polymorphism is literally many forms.
hth
Belsen
The right hand side of the assignment creates a new Hardball instance, and then the assignment makes the value of b a reference to the newly created Hardball object.
Jon
rmmd
Christiaanpmg
Ocrkass
When you develop a library of business objects, you use polymorphism to give each object a common set of properties, this is achieved by defining a base class and deriving business classes from it.
You have a lot to learn, eh