Inheritance 101 Extend your knowledge of inheritance to more easily extend your applications. Visual Basic developers had wanted inheritance for what seems like decades. The feature finally made it into VB.NET, making VB.NET an official object-oriented language. By now you are probably already using inheritance in your applications, but are you fully taking advantage of its potential? | " | Inheritance is an abstraction for sharing similarities among classes while preserving their differences.
| " |
As a consultant, I am frequently called into companies to perform code reviews of VB.NET and C# code. I truly enjoy this process because I have the benefit of seeing all of the many ways that applications can be architected and implemented. In going through the code, I have seen great use, and abuse, of inheritance in .NET applications. This article reviews what inheritance is, when and how to use it, and tips for getting the greatest benefit from this valuable feature. Sharing Similarities Inheritance is an abstraction for sharing similarities among classes while preserving their differences. It is easiest to fully grasp inheritance by way of an example. This example is a simple simulation that is loosely based on "WA-TOR", which was first introduced in a famous Scientific American magazine article by A. K. Dewdney. WA-TOR is a planet composed entirely of an ocean. It is shaped like a donut (the technical term for which is "toroid" - if you are into video games, think Halo). Though it is shown on a two-dimensional grid (see Figure 1) the movement is such that if something moves up off the top it shows up at the bottom, if it moves right off the edge it shows up on the left edge, and so on.  Figure 1: The WA-TOR simulation shows the movement of fish and sharks in a toroidal ocean.This simulation was recently presented as part of the MSDN "Object-Oriented Programming in .NET" event (http://www.msdnevents.com/). The code for the simulation is available at: www.microsoft.com/dcc. As with any object-oriented application, start by thinking about the entities or objects involved with the application. For example, this simulation application has an Ocean entity that represents the ocean space. This is represented with an Ocean class that manages a two-dimensional grid: Public Class Ocean Private myCell(0, 0) As Object Public Function getObject(ByVal p As Point) _ As Object Return myCell(p.x, p.y) End Function
Public Sub putObject(ByVal thing As Object) myCell(thing.Location.x, thing.Location.y)= _ thing End Sub . . . End Class
| & | | 
By: Deborah Kurata
Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing and developing successful applications.
Deborah is the author of several books, including Best Kept Secrets in .NET (Apress), Doing Objects in Visual Basic 6.0 (SAMS) and Doing Web Development: Client-Side Techniques (Apress). She is on the INETA Speaker’s Bureau, is a well-known speaker at technical conferences, and is a Microsoft Most Valuable Professional (MVP). After a hard day of coding and taking care of her family, Deborah enjoys blowing stuff up (on her XBox of course).
Some of the information in this article was obtained from her upcoming book, Doing Objects in VB 2005.
deborahk@insteptech.com | Fast Facts | | Inheritance allows us to think about our applications in a way that makes them easier to extend over time. This is key as more development teams are moving to an agile approach to iterative software development. | |
|