The Quick & Dirty .NET Guide to C#/VB OOP

Finally Polymorphism

Polymorphism is really pretty easy to understand. First off, it is derived from two Greek words "Poly" meaning many and "Morphism" means forms or shapes. In OOP this is taken to mean how one object is used as if it's another different object altogether. Key factors for polymorphism to take place are that the methods involved are overridable. Furthermore, upon polymorphism via inheritance, you are able to construct a uniquely new object based on a commonly derived method.

Any polymorphic methods in C# are declared with the keyword virtual or abstract , and the method overriding this specifies override . In VB you're dealing with Inheritable/Overridable methods. Contrary to overloading, overriding must include the identical features, including all the signatures (number of parameters) as the derived base class method.

[C#]
public class House
{
    public virtual string GetStatistics()
    {
        return ("My House is spacious");
    }
}

public class GuestHouse : House
{
    public override string GetStatistics()
    {
        return ("My House now a Guesthouse is roomy");
    }
}
public class Garage : House
{
    public override string GetStatistics()
    {
        return ("My House now a Garage is wide");
    }
}

[VB]
Public MustInherit Class House
    Public MustOverride Function GetStatistics() As String
Return ("My House is spacious")
End Function
End Class

Public Class Guesthouse : Inherits House

    Public Overrides Function GetStatistics() As String
        Return ("My House now a Guesthouse is roomy")
    End Function
End Class
Public Class Garage : Inherits House
    Public Overrides Function GetStatistics() As String
        Return ("My House now a Garage is wide")
    End Function
End Class

What was accomplished here through abstract polymorphism is that we overrode and modified the GetStatistics's return value. The other concept of Polymorphism - Inheritance-Based Polymorphism, work much along the same lines.

Our code to instantiate them is:

[C#]
House myHouse = new House ();
House myGuestHse = new Guesthouse ();
House myGarage = new Garage ();

Response.Write(myHouse.GetStatistics() + "<BR>");
Response.Write(myGuestHse.GetStatistics() + "<BR>");
Response.Write(myGarage.GetStatistics());

[VB]
Dim myHouse As House = New House ()
Dim myGuestHouse As House = New Guesthouse ()
Dim myGarage As House = New Garage ()
Response.Write(myHouse.GetStatistics() & "<BR>")
Response.Write(myGuestHouse.GetStatistics() & "<BR>")
Response.Write(myGarage.GetStatistics() & "<BR>")

And our results are:

So even though I declared my variables as a House, through polymorphism my House object takes on a different type and gets transformed into myGuestHouse and even myGarage! Think of the possibilities.

Phew! Not too bad. Well take heart that you have learned a great deal of OOP concepts and now should find it less cumbersome.

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous