Polymorphism allows a method of a class to be called in many formats means
Polymorphism
is the ability for class to provide different implimentations of methods that
are called by the same name . Polymorphism allows a method of a class to be
called in many formats means One
function behaves in different forms.In other words, "Many forms of a single object is called
Polymorphism."
function behaves in different forms.In other words, "Many forms of a single object is called
Real-world Example of Polymorphism
Example 1:
A teacher behaves students.
A teacher behaves his/her seniors.
Here teacher is an object but the attitude
is different in different situations.
Example 2
A person behaves the son in a house at the
same time that the person behaves an employee in an office.
Example 3
Your mobile phone, one name but many forms:
- As phone
- As camera
- As mp3 player
- As radio
Polymorphism Implementation:
Let’s understand some basic. If
a class A inherited by a class B
class A
{
public void
MethodA()
{
……………..
}
}
class B
{
public void
MethodB()
{
……………..
}
}
Then it is legal to write
A a = new B();
And we ca write
a.MethodA();
But it is incorrect to write
a.MethodB(); //error
Understanding
methods with same name in the Base class and Derived class:
In Shape class , let’s defined a Draw() method that draws shapes on screen.
class Shape
{
public void Draw()
{
Console.WriteLine(“ Drawing Shape…”);
}
}
We inherit another class Circle from the Shape Class, which also
contain a method Draw().
class Circle : Shape
{
public void Draw()
{
Console.WriteLine(“ Drawing Circle…”);
}
}
Here , we have Draw() method with the same signature in both the
Shape and the Circle classes. Now in our Main method, we wtrite
static void Main()
{
Circle the circle =new Circle();
thecircle.Draw();
}
Circle’s Draw() method is called and program will (expectedly)
display with warning the Draw() in the Circle hides the inherited Draw() method
of Shape class.
Output: Drawing Circle…
If we write in Main Methd
Shape theShape =new Circle();
theShape.Draw();
Shapes Draw() method is called and program will disply
Output : Drawing Shape…
Overriding
Methods – virtual and override keyword:
if we want to override the
Draw() method of Shape class in the Circle class, we have to mark the
Draw() method in the Shape(base) class as virtual and the Draw() method in the
Circle(derived) class as override.
class Shape
{
public virtual
void Draw()
{
Console.WriteLine(“ Drawing Shape…”);
}
}
class Circle : Shape
{
public override
void Draw()
{
Console.WriteLine(“ Drawing Circle…”);
}
}
Now we write in Main Methd
Shape theShape =new Circle();
theShape.Draw();
Circles Draw() method is called and program will disply
Output : Drawing Circle…
Since we have overridden the derived class method making the base
class method as virtual.
The
new Keyword
If in circle class , we do not want to override the Draw() method
, but we need to have a Darw() method, then marked the circle class method as
new and shape class method as virtual.
class Shape
{
public virtual void
Draw()
{
Console.WriteLine(“ Drawing Shape…”);
}
}
test
ReplyDelete