Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 48,845 times

Contents

Related Categories

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

DMarko1

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.

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 desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Re: [4341] The Quick &amp; Dirty .NET Guide to C#/VB OOP

    Posted by Yoenuts on 25 Jul 2006

    hello,


    I am very impressed by your tutorial as it finally allowed me to grasp the syntax behind OOP programming with .net.
    Only what I did not understand is how and where do I complile the .cs ...

  • Re: [4341] The Quick &amp; Dirty .NET Guide to C#/VB OOP

    Posted by ggorcsos on 22 Jun 2006

    Honestly I believe that the basic idea of the OOP was really great, but to be able to use it one really has to have the head as a water melon. There is too much theory, too many ther...

  • Posted by James Crowley on 20 May 2005

    Though from personal experience I'd say 99.99% of the time, you'd want private member variables and public properties... ;)

  • Posted by DMarko1 on 22 Apr 2005

    Hi Ehx,

    That's true, and it's funny that in all my other articles I always write all private variables with public properties. i.e. - [url="http://www.developerfusion.co.uk/show/4676/1/"]Building a...

  • Confusion about this article (Get ,Set)

    Posted by aalhussein on 22 Apr 2005

    After reading your article, http://www.developerfusion.com/show/4341/6/
    I got realy confused!!

    from what I know from the book below, I declare private property, then declare public (get , set )
    W...