Library articles and tutorials

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.

AddThis

Comments

  1. 25 Jul 2006 at 13:14

    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 to a dll? I cannot do it on the server. Do I do it on my local computer and then upload?


    http://www.developerfusion.co.uk/show/4341/5/

  2. 22 Jun 2006 at 06:29

    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 therms and the class theory really feels like puting each part of the program into a separate box and then figuring out how to drill a way between them. I started to learn C++ OOP at least 20 time and after a couple of weeks I did not even had an idea what it is about (note: I am not a proffesional programmer)...

    I believe that: "The use of a programming language should be as simple as a pie and the algorithm should be the part where people spend the most of their time..."

    When I do C# programming I do all public and it works great for me. Simply keep it simple!!!

    Is there any web page or book where I could find how to do Non-OOP C# programming?

    Sincerely,

    Gabor Gorcsos

     

     

     

  3. 20 May 2005 at 23:21

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

  4. 22 Apr 2005 at 18:00

    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. - Building a Full-Featured Custom DataGrid Control. It's just one of those overlooked things. Oh well.

  5. 22 Apr 2005 at 17:26

    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 )
    What you presented in your article is you declared public property,then  made the (get,set) private)


    I have put this simple comparison code , so please advice your point, What is the logic behind reversing the modifier(public to private and vis versa).
    thanks
    Ehx
     
        // what is in the book (Begining Asp.Net Database using C# p 321)
           private string Country;
           public string Country
           {
               get{return _Country;}
               set{
    Country= value;}
           }


                  // what is in web article ( in your article)
           public string Name;
           private string _Name
           {
               get{return Name;}
               set{Name= value;}
           }

Leave a comment

Sign in or Join us (it's free).

Related discussion

Related jobs

Events coming up

  • Oct 14

    What’s New in Visual Studio 2008 Service Pack 1?

    Birmingham, United Kingdom

    “Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.