What is Polymorphism?
Polymorphism means one name for many forms.
Polymorphism means one object behaving in multiple forms.
In other words, "Many forms of a single object is called Polymorphism."
One function behaves differently in multiple forms.
Real-World Example of Polymorphism
Example 1
A teacher behaves to student.
A teacher behaves to his/her seniors.
Here teacher is an object but the attitude is different in each situation.
There are two types of polymorphism:
1. Static or Compile-time Polymorphism
2. Dynamic or Runtime Polymorphism.
3.
Static or Compile-time Polymorphism
In Static Polymorphism the decision is made at compile-time.
Which method is to be called is decided at compile-time only.
Method overloading is an example of this.
Compile-time polymorphism is method overloading, where the
compiler knows which overloaded method it is going to call.
Method overloading is a concept where a class can have more than one
method with the same name and different parameters.
The compiler checks the type and number of parameters passed to the
method and decides which method to call at the compile-time and it will
give an error if there are no methods that matches the method signature
of the method that is called at the compile-time.
namespace MethodOverloadingByManishAgrahari
{
class Program
{
public class TestOverloading
{
public void Add(string a1, string a2)
{
Console.WriteLine("Adding Two String :" + a1 + a2);
}
public void Add(int a1, int a2)
{
Console.WriteLine("Adding Two Integer :" + a1 + a2);
}
}
static void Main(string[] args)
{
TestOverloading obj = new TestOverloading();
obj.Add("Manish " , "Agrahari");
obj.Add(5, 10);
Console.ReadLine();
}
}
Dynamic or Runtime Polymorphism
Run-time polymorphism is done by method overriding.
Method overriding allows us to have methods in the base and derived
classes with the same name and the same parameters.
By runtime polymorphism we can point to any derived class from the
object of the base class at runtime that shows the ability of runtime
binding.
See the following example:
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("Show From Base Class.");
}
}
public class Derived : Base
{
public override void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBase;
objBase = new Base();
objBase.Show();// Output ----> Show From Base Class.
objBase = new Derived();
objBase.Show();//Output--> Show From Derived Class.
Console.ReadLine();
}
}
}
Virtual Modifier
According to MSDN, "The virtual keyword is used to modify a method,
property, indexer or event declaration, and allow it to be overridden in a
derived class.".
A virtual method is a method whose behavior can be overridden in a
derived class.
Virtual method allows declare a method in base class that can be
redefined in each derived class.
When a virtual method is invoked, the run-time type of the object is
checked for an overriding member. The overriding member in the most
derived class is called, that might be the original member, if no derived
class has overridden the member.
See the following example:
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("Show From Base Class.");
}
}
public class Derived : Base
{
//the keyword "override" change the base class method.
public override void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBaseRefToDerived = new Derived();
objBaseRefToDerived .Show();//Output--> Show From Derived
Class.
Console.ReadLine();
}
}
}
Inheritance in VB.NET
Inheritance is the idea that one class, called a subclass, can be based on
another class, called a base class. Inheritance provides a mechanism for
creating hierarchies of objects. For example, a dog is a mammal and a
collie is a dog. Thus the dog class inherits the properties and methods of
the mammal class, and the collie class inherits from
both dog and mammal.
Support for implementation inheritance is perhaps the biggest change in
Visual Basic.NET. Table shows the new keywords that have been added
along with new statements and methods.
Public Class Person
Protected c_sFirstName as String
Protected c_sLastName as String
MustOverride ReadOnly Property ClassName() as String
Get
ClassName = "Person"
End Get
End Property
NotOverridable ReadOnly Property BaseClassName() as String
Get
BaseClassName = "Person"
End Get
End Property
Overidable Public Property FirstName() as String
Get
FirstName = c_sFirstName
End Get
Set(ByVal Value as string)
c_sFirstName = Value
End Set
End Property
Overidable Public Property LastName() as String
Get
LastName = c_sLastName
End Get
Set(ByVal Value as string)
c_sLastName = Value
End Set
End Property
Overridable Sub Speak()
Console.WriteLine("I am " & c_sFirstName & " " & c_sLastName)
Console.WriteLine(" and I am a Person.")
End Sub
End Class