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 > Preventing Inheritance

Preventing Inheritance

At the opposite end of the inheritability spectrum from the abstact classes and members that must be inherited are sealed classes. The syntax to seal a class is straightforward. Just mark a class with the NotOverridable keyword and other classes can’t inherit from it:

Public NotOverridable Class mySealed

Classes are easy, but those tricky keywords new and override get involved when you want to seal individual methods within a class. Here’s how that works:

image

Put on Your Thinking Hat

image

It might seem that only being able to mark a method as NotOverridable when you use the Overrides keyword is a limitation, but in fact the definition of DontTouchMe() in myNew above is effectively sealed. Do you know why? What keyword would you need to add to make it inheritable?


  

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