Hi Guys This post is about implementing poylmorphism in c# and role of interface in c#
Polymorphism
Why?
Lets see by an example
public class Abstract
{
public void myfunction()
{
Console.Write("This is Abstract");
}
}
public class Inherited : Abstract
{
public override void myfunction()
{
Console.Write("This is Function Class");
}
}
class Program
{
static void Main(string[] args)
{
Abstract Abs = new Abstract();
Abs = new Inherited();
Abs.myfunction();
Console.Read();
}
}
Now when you call this you will see the following Output
"This is Function Class"
But with a warning:
Warning 1
'TestConsole.Function.myfunction()' hides inherited
member 'TestConsole.Abstract.myfunction()'. Use the
new keyword if hiding was intended. C:\Documents
and Settings\vivek\My Documents\Visual Studio
2008\Projects\TestConsole\TestConsole\Program.cs
23 22 TestConsole
which stated that if you inherit child class from parent class with having function of same signature the child
class function will be hided by parent class function.To avoid this hide of child class function by parent
class we need to use polymorphism.as follows:
in the abstract class use the following signature of function:
public virtual void myfunction()
in the inherited class use the following singature of function:
public override void myfunction()
and now if you instantiate any of the class with abstract class's object it will override by inherited class function.
What is a interface in c# lets try it with example
I create a interface as follows:
interface Iabstract
{
void myfunction();
}
Now i implement this with Inherited class as follows:
public class Inherited : Abstract, Iabstract
{
public override void myfunction()
{
Console.Write("This is Function Class");
}
}
and call an instance of interface which references to inherited class in Main() as follows:
Iabstract objabc = new Inherited();
What should be the output?
"This is Function Class"
if you even remove the polymorphism mechanism from this still the interface instance will show the same output.
Why is it so? because interface is contract which calls functions of those classes on which its being implemented neither more nor less than that.
Hope you will enjoy this article...Thanks
Polymorphism
Why?
Lets see by an example
public class Abstract
{
public void myfunction()
{
Console.Write("This is Abstract");
}
}
public class Inherited : Abstract
{
public override void myfunction()
{
Console.Write("This is Function Class");
}
}
class Program
{
static void Main(string[] args)
{
Abstract Abs = new Abstract();
Abs = new Inherited();
Abs.myfunction();
Console.Read();
}
}
Now when you call this you will see the following Output
"This is Function Class"
But with a warning:
Warning 1
'TestConsole.Function.myfunction()' hides inherited
member 'TestConsole.Abstract.myfunction()'. Use the
new keyword if hiding was intended. C:\Documents
and Settings\vivek\My Documents\Visual Studio
2008\Projects\TestConsole\TestConsole\Program.cs
23 22 TestConsole
which stated that if you inherit child class from parent class with having function of same signature the child
class function will be hided by parent class function.To avoid this hide of child class function by parent
class we need to use polymorphism.as follows:
in the abstract class use the following signature of function:
public virtual void myfunction()
in the inherited class use the following singature of function:
public override void myfunction()
and now if you instantiate any of the class with abstract class's object it will override by inherited class function.
What is a interface in c# lets try it with example
I create a interface as follows:
interface Iabstract
{
void myfunction();
}
Now i implement this with Inherited class as follows:
public class Inherited : Abstract, Iabstract
{
public override void myfunction()
{
Console.Write("This is Function Class");
}
}
and call an instance of interface which references to inherited class in Main() as follows:
Iabstract objabc = new Inherited();
What should be the output?
"This is Function Class"
if you even remove the polymorphism mechanism from this still the interface instance will show the same output.
Why is it so? because interface is contract which calls functions of those classes on which its being implemented neither more nor less than that.
Hope you will enjoy this article...Thanks











2 comments:
wrong example ....
Its the correct one,but this warning was only available untill vs2008 after this warning has been removed.
Post a Comment