Defining Classes That Cannot Be Subclassed

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 10.  Inheritance and Polymorphism

Defining Classes That Cannot Be Subclassed

When a branch of your class hierarchy comes to a point and you want to ensure that no further inheritance occurs, you can define a final class.

A final class is a class that has the NotInheritable modifier in the class statement. For example, if we wanted to indicate that no child class can be derived from the SingleEngineLand plane, we could introduce the NotInheritable keyword, revising the class statement as follows :

 Public NotInheritable Class SingleEngineLand 

With that done, consumers can no longer subclass SingleEngineLand planes.

Using modifiers like NotInheritable or MustInherit is like using access specifiers Public, Private, Protected, or Friend, in that you can elect not to use them. Make everything public and avoid modifiers altogether if you want to, but the end result is code that is less explicit and controlled. Explicit controlled code is more likely to yield manageable, less complex code.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net