polymorphism definition |
POLYMORPHISM
The process
of re-implementation of parent class member in child class with same name but different behavior, state with the help of keyword ‘virtual’ and ‘Override’.
RULE TO PERFORMING POLYMORPHISM
We have to
follow some rule to perform polymorphism.
1. Inheritance should develop between classes to perform
polymorphism.
2. Parent class member should rewrite in child class with new
behavior.
3. Which the member should call, constructor will decide, on the
base of constructor the member of either child or parent will call.
4. For the permission sake parent class member should declare
virtual like this,
Public class Parent
{
Public virtual void member ( )
}
When a
member declare virtual its mean it override able in its child class.
5. Child member should declare with keyword override like this,
Public class child: Parent
{
Public override void member ( )
}
METHOD OVERRIDING
The redefining
of parent class methods in child class with same name with different behavior and
state. The parent class should virtual and the class member should define
override.
SYNTAX
Public class Parent
{
Public virtual void Testmethod
( )
{
Task;
}
}
Public class Child: Parent
{
Public override void
Testmethod ( )
{
Task; //changed behavior
}
}
CALLING METHOD SYNTAX
Parent obj = new child ( );
Obj. Testmethod ( );
// here the constructor if child is calling thus the Testmethod of
child will execute
DIFFERENCE BETWEEN OVERLOADING AND OVERRIDING
Overloading
|
Overriding
|
0 comments:
Post a Comment
Please do not enter any spam link in the comment box