Others have already explained the theory behind Inheritance
Polymorphism so let me illustrate with a (hurriedly constructed and
basic) example:
Let's assume that the Library lends various types of items : Books,
Videos and Magazines, enumerated in an Enum "assetType". The asset
types are all classes that inherit from the base class LibraryAsset.
LibraryAsset
|
__________________
| | |
Book Video Magazine
When the function "ReturnAssetToLibrary" is called, we do not know
what is the actual type of the asset. Therefore we create a variable
of the base class type. And then instantiate the correct type on the
basis of some conditions. This is the essence of Inheritance
Polymorphism.
---
Public Sub ReturnAssetToLibrary(ByVal dueDate As DateTime, byval aType
as assetType)
Dim myAsset As LibraryAsset
Select Case aType
Case assetType.Book
myAsset = new Book()
Case assetType.Video
myAsset = new Video()
Case assetType.Magazine
myAsset = new Magazine()
End Select
myAsset.DueDate = dueDate
'Fine calculation may differ based on the type - it may be greater
for magazines and videos.
'In this case the correct method of the derived class would be
called.
Dim amountDue As Double = myAsset.CalculateFineTotal()
Console.WriteLine("Asset: {0}", amountDue.ToString())
End Sub
---
There are much better examples out there! Feel free to research this
rather interesting topic.