Tuesday 13 September 2011

Polymorphism

It is the capability to have methods and properties in multiple classes that have
the same name can be used interchangeably, even though each class
implements the same properties or methods in different ways.

Let us understand what is polymorphism with the following example.
Now we will consider the maintenance of company information which includes
employee and customer details. A person (base class) is defined to hold the
common information of the individual. The base class maintains contact
information of the person and manipulates the data. It contains save method to
update the contact information of the person.

Two derived classes, Employee and Customer are used to process the employee
and customer details. These two derived classes inherits person class to
manipulate identity of the person instead of rewriting the same code again in the
derived classes. Since each derived class needs to use the displayinfo method to
display the additional information with the contact details, the displayinfo method
of the base class will be overwritten in the respective derived class using
overrides keyword. The overriding member signature in the derived class must
be as the base class signature.
Signature includes the member type, member name, parameters datatype and
return datatype.

Look into the following example and see the implementation of base class
(Person) and the derived class (Employee), observe the changes in the
displayInfo method in both the classes.

Ex:-
Class Person
Private Name As String
Private Address As String
Public ReadOnly Property PName() As String
Get
Return Name
End Get
End Property
Public ReadOnly Property PAddress() As String
Get
Return Address
End Get
End Property
Public Overridable Function DisplayInfo() As String
Dim msg As String
msg = "Name : " & Name & vbCrLf
msg = msg & "Address : " & Address & vbCrLf
Return msg
End Function
Public Sub Save(ByVal parName As String, ByVal parAddress As String)
Name = parName
Address = parAddress
End Sub
End Class
class Person
{
private string Name;
pPrivate string Address;
public string PName()
{
get
{
return Name;
}
}
public string Paddress
{
get
{
return Address;
}
}
public virtual string DisplayInfo()
{
string msg;
msg = "Name : " + Name;
msg = msg + "Address : " + Address;
return msg;
}
public void Save(string parName, string parAddress)
{
Name = parName;
Address = parAddress;
}
}
Derived Class:-
Class Employee
Inherits Person
Public EmpId As Integer
Private Sal As Double = 0
Public Basic As Double
Public Allowance As Double
Public Deductions As Double
Public Overrides Function DisplayInfo() As String
Dim msg As String
msg = MyBase.DispalyInfo()
msg = msg & "ID : " & EmpId.ToString & vbCrLf
msg = msg & "Basic : " & Basic.ToString & vbCrLf
msg = msg & "Allowances : " & Allowance.ToString & vbCrLf
msg = msg & "Deductions : " & Deductions.ToString & vbCrLf
msg = msg & "Net Salary : " & Sal.ToString & vbCrLf
return(msg)
End Function
Public ReadOnly Property Salary() As Double
Get
Return Sal
End Get
End Property
Public Sub ProcessSalary()
Sal = Basic + Allowance - Deductions
End Sub
End Class
class Employee: Person
{
public int EmpId;
private double Sal = 0;
public double Basic;
public double Allowance;
public double Deductions;
public override string DisplayInfo()
{
string msg ;
msg = base.DispalyInfo();
msg = msg + "ID : " + EmpId.ToString;
msg = msg + "Basic : " + Basic.ToString;
msg = msg + "Allowances : " + Allowance.ToString;
msg = msg + "Deductions : " + Deductions.ToString;
msg = msg + "Net Salary : " + Sal.ToString ;
return(msg)
}
public double Salary
{
get
{
return Sal;
}
}
public void ProcessSalary()
{
Sal = Basic + Allowance – Deductions;
}
}
The following keywords are used in achieving polymorphism.
• Overridable:- A method or property defined in the base class can be
overwritten in the derived class.
• Overrides:- Indicates the method or property is being overwritten in the
derived class.
• Mustoverrides:- A method or property defined in the base class must be
overwritten in the derived class
• Notoverridable:- A method or property defined in the base class must not
be overwritten in the derived class.
Virtual Members :-Virtual members are those that can be overridden and
replaced by the derived classes. They are declared with Overridable keyword.
The methods or properties which doesn’t contain overridable keyword are called
Non-Virtual members.
For ex:- DisplayInfo method is an virtual method of Person base class, because it
has been overwritten in the Employee derived class with new DisplayInfo method
with same signature.
Restricting Polymorphism :-
We have already seen polymorphism is implemented with Overridable and
Overrides keyword. Some situation arises where we need to break the
polymorphism effect of the base class method. It means changing the signature
of the base class member while overriding it in the derived class violates the
polymorhism rule “signatures must be same”. Restriction is required to
completely change the implementation of the member in the derived class. So it
restricts the polymorphism of the base class member with new implementation in
the derived class. The Shadow keyword used to implement this concept.
Take an example shown below which contains two classes 1) Person Class 2)
Employee class.
Person class is a base class contains method to be overwritten from the derived
class. The signature of the overridable method is
Public Overridable Function DisplayInfo() as String
Employee class is a derived class which inherits person class and restricts the
implementation of the polymorphism method displayinfo of the base class with
shadows keyword.
From the above explanation, we can observe that the signature of the DisplayInfo
member is changed from function in the base class to the subroutine in the
derived class with complete change in the implementation.
Ex:-
Base Class:-
Public Shadows Sub DisplayInfo()
Class Person
Public Name As String
Public Address As String
Public Overridable Function DisplayInfo() As String
Dim msg As String
msg = Name & vbCrLf
msg = msg & Address & vbCrLf
Return msg
End Function
Public Sub Save(ByVal parName As String, ByVal parAddress As String)
name = parName
address = parAddress
End Sub
End Class
class Person
{
public string Name;
public string Address ;
public virtual string DisplayInfo()
{
string msg;
msg = Name ;
msg = msg + Address;
return msg;
}
public void Save(string parName, string parAddress)
{
name = parName;
address = parAddress;
}
}
Derived Class:-
Class Employee
Inherits Person
Public EmpId As Integer
Private Sal As Double = 0
Public Basic As Double
Public Allowance As Double
Public Deductions As Double
Public Shadows Sub DisplayInfo()
Dim msg As String
msg = MyBase.DisplayInfo("")
msg = msg & "ID : " & EmpId.ToString & vbCrLf
msg = msg & "Basic : " & Basic.ToString & vbCrLf
msg = msg & "Allowances : " & Allowance.ToString & vbCrLf
msg = msg & "Deductions : " & Deductions.ToString & vbCrLf
msg = msg & "Net Salary : " & Basic.ToString & vbCrLf
MsgBox(msg)
End Sub
Public ReadOnly Property Salary() As Double
Get
Return Sal
End Get
End Property
Public Sub ProcessSalary()
Sal = Basic + Allowance - Deductions
End Sub
End Class
class Employee: Person
{
public int EmpId ;
private double Sal = 0;
public double Basic;
public double Allowance;
public double Deductions;
public new void DisplayInfo
{
string msg;
msg = Base.DisplayInfo("");
msg = msg + "ID : " + EmpId.ToString;
msg = msg + "Basic : " + Basic.ToString;
msg = msg + "Allowances : " + Allowance.ToString;
msg = msg + "Deductions : " + Deductions.ToString;
msg = msg + "Net Salary : " + Basic.ToString;
MessageBox.Show(msg);
}
public double Salary
{
get
{
return Sal;
}
}
public void ProcessSalary()
{
Sal = Basic + Allowance – Deductions;
}
}

No comments:

Post a Comment