RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 129, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


Polymorphism Rocks!

I have known what inheritance and polymorphism were for quite some time, but I never really deeply implemented polymorphism into my programming until now. 

Why? I have a program that has objects that are similar and I have decided to abstract them for a lower amount of coding. A lower amount of code to write and maintain means a quicker solution.  I tend to think of it like Database design; code redundancy is removed where it can be.

This is how I did it:

  1. I created the base class with MustInherit (Public MustInherit Class EC_Object).
  2. I refered the derived (sub-)class to the base class (Public Class EC_User : Inherits EC_Object).
  3. I migrated items that were universal to the base class and removed anything that wasn't going to override from the derived class. 
  4. I changed all of the private declarations in the base class to protected if the derived class needed to use the variable, property or procedure.
  5. Then I realized that I could set the MustOverride on Subprocedures and functions.  That really helped me keep some top level code private and implement needed functionality of derived classes.
    • Example: I have a procedure that I wanted to abstract into the base object that calls for information from the object.  I thought that I was not going to be able to do it because I had to pass the procedure information about a derived object.  What I realized I could do was declare Public MustOverride Function GetInformation() As String in the base, and then add the function to the derived class: Public Overrides Function GetInformation() As String.  Then I could write my code as normal in the derived class and when the base code calls for the GetInformation() Function, it will actually use the overriden function in the derived class.
    • Code in base class:
      • Public MustOverride Function GetInformation() As String

    • Code in derived class:

      • Public Overrides Function GetInformation() As String
            Dim message As New System.Text.StringBuilder
            message.Append("Here is some information about that particular user:" & vbCrLf & _
              vbTab & "Name: " & FullName() & vbCrLf & _
              vbTab & "Email: " & Email() & vbCrLf & _
              vbTab & "Create Date: " & CreateDate() & vbCrLf & _
              vbTab & "Last Access Date: " & LastAccessDate() & vbCrLf & _
              vbTab & "Comments: " & Comments() & vbCrLf & _
              vbTab & "Log In Time: " & LogInTime() & vbCrLf)
            'return
            Return message.ToString()
        End Function

  6. I implemented the keyword Overrides (in the derived) on all of the objects that override the base class.

That is pretty much all it took to implement.  Just remember abstraction, inheritance and polymorphism all equal one important concept = less code redundancy, and less code period.

'Nough said.

posted on Thursday, November 04, 2004 1:02 PM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: