Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Inheritance 101

Extend your knowledge of inheritance to more easily extend your .NET applications.


advertisement
isual 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?

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 provides some 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.

This simulation was recently presented as part of the MSDN "Object-Oriented Programming in .NET" event. 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:

 
Figure 1. Simulated Movement: The WA-TOR simulation shows the movement of fish and sharks in a toroidal ocean.

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


Author's Note: This simulation example does not provide guidance on good naming conventions.

The Ocean class also provides the functions defining the rules for movement within the ocean. For example, the code to move up from a point is as follows:

Public Function northOf(ByVal p As Point) As Point Dim y As Integer = p.y - 1 If (y < 1) Then y = myHeight End If Return New Point(p.x, y) End Function


Inheritance is an abstraction for sharing similarities among classes while preserving their differences.
Using this as an example, you can easily write the code to move left, right, and down.

In this simulation, there are three types of entities that can exist within the ocean: water, fish, and sharks (see Figure 1). Each of these entities will reside within a cell of the two-dimensional ocean grid. So each of these entities needs a property to identify its location, defined with an (x, y) point, and an image defining how the entity will appear visually on the grid. These entities are implemented as classes in the application with Location and Image properties. As an example, the Water class is as follows:



Public Class Water Private myLocation As Point Private Shared myImage As Image _ = Image.FromFile("../water.jpg") Public ReadOnly Property image() As Image Get Return myImage End Get End Property Public Property location() As Point Get Return myLocation End Get Set(ByVal Value As Point) myLocation = Value End Set End Property Public Sub New(ByVal location As Point) myLocation = location End Sub End Class


This code defines the two properties and a constructor for creating a new water entity at a particular point in the ocean grid.

You can copy and paste this code to create a very similar looking Fish class. Using a copy and paste technique to share similarities between classes is jokingly referred to as "clipboard inheritance." The resulting Fish class is as follows:

Public Class Fish Private myLocation As Point Private Shared myImage As Image _ = Image.FromFile("../fish.jpg") Public ReadOnly Property image() As Image Get Return myImage End Get End Property Public Property location() As Point Get Return myLocation End Get Set(ByVal Value As Point) myLocation = Value End Set End Property Public Sub New(ByVal location As Point) myLocation = location End Sub End Class


Unlike the Water class, however, the Fish class needs an additional method. For the simulation to work properly, the Fish need to move. The movement rules for the fish in this simulation are that a fish looks in a random direction for an empty location. If the location contains only water, the fish spawns a new fish at its current location and moves into the empty location. If the location contains something else, the fish stays in its current location. This Move method is as follows:

Public Overrides Sub Move(ByVal newOcean As Ocean) Dim n As Integer = Rnd.Next(1, 5) Dim p As Point Select Case n Case 1 p = newOcean.northOf(myLocation) Case 2 p = newOcean.eastOf(myLocation) Case 3 p = newOcean.southOf(myLocation) Case 4 p = newOcean.westOf(myLocation) End Select If (newOcean.getObject(p).GetType. _ Equals(GetType(Water))) Then newOcean.putObject(New Fish(myLocation)) myLocation = p newOcean.putObject(Me) Else newOcean.putObject(Me) End If End Sub


You can then repeat the "clipboard inheritance" process to define the properties in a Shark class. But the fact that the property code was repeated in all three classes should give you an indication that you have similarities among these classes. And sharing similarities is part of what inheritance is all about.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap