Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

12. OOA&D > Polymorphism & Casting

Polymorphism & Casting

Polymorphism isn’t the same as casting. When you cast a type to another, it behaves like the new type. The code that’s executed is that of the type to which it is cast, not the original type.

image

Let’s look at an example: Here’s a simple class hierarchy with two classes, each exposing a single member, a property called Name. The Parent class returns the String “Parent” in the get accessor, while the Child class returns the String “Child”. (We’ll examine redefining inherited members in detail in the next section.)

Public Class Parent
 Public Property Name As String
  Get
   Return "Parent"
  End Get
 End Property
End Class

image

On Your Own

image

image


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint