Here it is, ladies and gentlemen, part 3 of the 4 part series on the 4 major principles of object-oriented programming: data abstraction and encapsulation we have already talked about, polymorphism will be last and today we have inheritance.
Objects can relate to eachother with either a “has a”, “uses a” or an “is a” relationship. “Is a” is the inheritance way of object relationship. The example of this that has always stuck with me over the years is a library (I think I may have read it in something Grady Booch wrote). So, take a library, for example. A library lends more than just books, it also lends magazines, audiocassettes and microfilm. On some level, all of these items can be treated the same: All four types represent assets of the library that can be loaned out to people. However, even though the 4 types can be viewed as the same, they are not identical. A book has an ISBN and a magazine does not. And audiocassette has a play length and microfilm cannot be checked out overnight.
Each of these library’s assets should be represented by its own class definition. Without inheritance though, each class must independently implement the characteristics that are common to all loanable assets. All assets are either checked out or available for checkout. All assets have a title, a date of acquisition and a replacement cost. Rather than duplicate functionality, inheritance allows you to inherit functionality from another class, called a superclass or base class.
Let us look at loanable assets base class. This will be used as the base for assets classes such as book and audiocassette:
Public Class LibraryAsset
Private _title As String
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal Value As String)
_title = Value
End Set
End Property
Private _checkedOut As Boolean
Public Property CheckedOut() As Boolean
Get
Return _checkedOut
End Get
Set(ByVal Value As Boolean)
_checkedOut = Value
End Set
End Property
Private _dateOfAcquisition As DateTime
Public Property DateOfAcquisition() As DateTime
Get
Return _dateOfAcquisition
End Get
Set(ByVal Value As DateTime)
_dateOfAcquisition = Value
End Set
End Property
Private _replacementCost As Double
Public Property ReplacementCost() As Double
Get
Return _replacementCost
End Get
Set(ByVal Value As Double)
_replacementCost = Value
End Set
End Property
End Class
This LibraryAsset is a superclass, or base class, that maintains only the data and methods that are common to all loanable assets. Book, magazine, audiocassette and microfilm will all be subclasses or derived classes or the LibraryAsset class, and so they inherit these characteristics. The inheritance relationship is called the “is a” relationship. A book “is a” LibraryAsset, as are the other 3 assets.
Let’s look at book and audiocassette classes that inherit from out LibraryAsset class:
Public Class Book
Inherits LibraryAsset
Private _author As String
Public Property Author() As String
Get
Return _author
End Get
Set(ByVal Value As String)
_author = Value
End Set
End Property
Private _isbn As String
Public Property Isbn() As String
Get
Return _isbn
End Get
Set(ByVal Value As String)
_isbn = Value
End Set
End Property
End Class
Public Class AudioCassette
Inherits LibraryAsset
Private _playLength As Int16
Public Property PlayLength() As Int16
Get
Return _playLength
End Get
Set(ByVal Value As Int16)
_playLength = Value
End Set
End Property
End Class
Now, lets create an instance of the book class so we can record a new book into the library inventory:
Dim myBook As Book = New Book
myBook.Author = "Sahil Malik"
myBook.CheckedOut = False
myBook.DateOfAcquisition = #2/15/2005#
myBook.Isbn = "0-316-63945-8"
myBook.ReplacementCost = 59.99
myBook.Title = "The Best Ado.Net Book You'll Ever Buy"
You see, when we create a new book, we have all the properties of the LibraryAsset class available to us as well, because we inherited the class. Methods can be inherited as well. Let’s add a few methods to our LibraryAsset class:
Public Class LibraryAsset
' Pretend the properties listed above are right here
Public Sub CheckOut()
If Not _checkedOut Then _checkedOut = True
End Sub
Public Sub CheckIn()
If _checkedOut Then _checkedOut = False
End Sub
End Class
Now, our “myBook” we created above automatically inherited these methods, and we didn’t even have to touch the Book class in order for it to happen. The book and audiocassette classes above automatically inherited the abilities to be checked out and checked in. In our “myBook” above, now we can check the book out by calling “myBook.CheckOut()”. Simple! One of the most powerful features of inheritance is the ability to extend components without any knowledge of the way in which a class was implemented.
Declaration options, such as Public and Private, dictate which members of a superclass can be inherited. For more information on this, see the Declaration Option section of Eric's post.
Currently listening to: Rollin' (The Ballad Of Big & Rich) - Big & Rich