Polymorphism
What is polymorphism?
The meaning of the word polymorphism is something like one name, many forms.
How does C# implement polymorphism?
Defination:providing many functionalities with a single insatance name
Polymorphism manifests itself in C# in the form of multiple methods having the same name.
From a practical programming viewpoint, polymorphism manifests itself in two distinct forms in C#:
Fucntion Overloading
Function Overriding
Fucntion Overloading:
Ø Overloading means having more than one method with the same name, but a different number(list) of arguments or diiferent type of arguments in the same class or in a combination of base and derived classes .
Example:
class Projector
{
public static void Display( string str )
{
System.Console.WriteLine( str );
}
public static void Display( int number )
{
System.Console.WriteLine( number);
}
public static void Main()
{
Projector.Display( "Hello" );
Projector.Display( 1234 );
}
}
Output:
Hello
1234
Examples.for Method Overloading possibilities:
1)void calaculate();
2)void calaculate(int a); //ok:u can overload
3)void calaculate(string a, int b);//ok u can overload
4)void calaculate(string h, int t);//error 3 and 4 can’t overload,because signatures are same
5)int calaculate(int g); //error:2 and 5 can't overload based on return type only
//ref method
6)void calaulate(ref int x);// u can overload with all the above methods but u can’t overload with 7th method.
// out method
7)void calaculate(out int x);// u can not overload 6th and 7th methods,but with other methods u can overload 6th and 7th methods individually.
Function Overriding
2)Function Overriding:
Overriding means have a method with the same name and the same signature in the base class and derived classes.
To override a method we have to use “virtual” keyword for base class method and override keyword for derived class method.
Example:
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public override void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 ;
d1= new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
When we declare a virtual method, it must contain a method body. Other wise the compiler will generate an error.
Remember that, since virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it not possible to declare a static method as virtual in C#.
Similarly the private methods are also not possible to declare virtual, since they can’t override inside a derived class.
Method overriding is a feature that allows you to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference..
Both methods should have the same access level; the same return type, the same name and same method signature.
Let's understand this through small examples.
Example for NEW keyword:
We can override a method without declaring the base class method as 'virtual'. We can use the modifier 'new' to tell the compiler that the derived class method 'hides' the base class method.
class Base
{
public void Display()
{
Console.WriteLine("Base method");
}
}
class Derived:Base
{
new public void Display()
{
Console.WriteLine("Drived method");
}
}
class HideTest
{
public static void Main()
{
Base b=new Base();
b.display();
Derived d=new Derived();
d.Display();
}
}
Output:
Base method
Derived method
Example1:
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class Demo
{
public static void Main()
{
BC b; // creating reference variable b of type BC
b = new BC(); //creating object of type BC and assign its reference to reference variable b
b.Display();
}
}
Output
BC::Display
The above program compiles and runs successfully to give the desired output. It consists of a base class BC and a derived class DC. Class BC consists of function Display(). Class DC hides the function Display() it inherited from the base class BC by providing its on implementatin of Display(). Class Demo consists of entrypoint function Main(). Inside Main() we first create a reference b of type BC. Then we create an object of type BC and assign its reference to reference variable b. Using the reference variable b we invoke the function Display(). As expected, Display() of class BC is executed because the reference variable b refers to the object of class BC.
Now we add a twist of line to the above program.
Example 2
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
}
}
Output
BC::Display
BC::Display
Here we are creating an object of Derived class DC and storing its reference in the reference variable b of type BC. This is valid in C#. Next, using the reference variable b we invoke the function Display(). Since b contains a reference to object of type DC one would expect the function Display() of class DC to get executed. But that does not happen. Instead what is executed is the Display() of BC class. That's because the function is invoked based on type of the reference and not to what the reference variable b refers to. Since b is a reference of type BC, the function Display() of class BC will be invoked, no matter whom b refers to. Take one more example.
Example 3
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : BC
{
new public void Display()
{
System.Console.WriteLine("TC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
b = new TC();
b.Display();
}
}
Output
BC::Display
BC::Display
BC::Display
The output of the above program is a receipt of the fact that no matter to whom base class reference b refers, it invokes the functions of the class that matches its type.
But actually , If b contains the reference to a particular derived class object, then its supposed to invoke the function of that class.
Well, C# helps us do this by the usage of keywords virtual and override as shown in the following program.
Example 4
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
}
}
Output
BC::Display
DC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the Derived class's implementation of Display() is decorated with the modifier override. Doing so enables C# to invoke functions like Display() based on objects the reference variable refers to and not the type of reference that is invoking the function. Hence in the above program when b refers to the object of class BC it invokes Display() of BC and then when b refers to the object of class DC it invokes Display() of class DC. Let's see if this holds true for the third generation of derived classes. Take the following program.
Example 5
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : DC
{
public override void Display()
{
System.Console.WriteLine("TC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
b = new TC();
b.Display();
}
}
Output
BC::Display
DC::Display
TC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the implementation of Display() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable b and invoke Display(). The rite versions of Display get invoked based on the object the reference variable refers to. Time for a tiny teaser! Guess what the output would be in the following program?
Example 6
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : DC
{
}
class Demo
{
public static void Main()
{
BC b;
b = new TC();
b.Display();
}
}
Output
DC::Display
Since TC has no implementation of Display(), it inherits Display() from DC as TC is derived from DC. Hence Display() from Derived class DC gets executed. It's as if the derived class TC looked like this:
class TC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
to the compiler. Take one more example. Guess what its output will be.
Example 7
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : DC
{
public new void Display()
{
System.Console.WriteLine("TC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new TC();
b.Display();
}
}
Output
DC::Display
Agreed that TC defines its own new version of Display(). But its version of display is not invoked as Display() of TC does not override the Display() of the base class. With this understood we are done with Method overriding in C#.
Differences between Function overriding and Function overloading
Function Overloading Function Overriding
1) Both method names should be same,but signature should not be same.
1) Both method names,and signature and retun type should be same
2) In Overloading we will not use any virtual and override keywords 2) In overriding we have to use Virtual and Override keywords
3)Overloading we can implement with in single class or combination
of base and derived classes.
3)We can implement Overriding in base class and derived class but we can not implement in a single class
No comments:
Post a Comment