Inheritance

 <  Day Day Up  >  

A class can inherit members from another class. For example, a Square class can inherit members from a Shape class.

 Class Shape   Public x, y As Integer End Class Class Square   Inherits Shape   Public Width, Height As Integer   Public Sub New(ByVal x As Integer, ByVal y As Integer, _       ByVal Width As Integer, ByVal Height As Integer)     Me.x = x     Me.y = y     Me.Width = Width     Me.Height = Height   End Sub End Class 

In this example, the Square class inherits members named x and y from the Shape class. The Shape class is called the base class of the Square class, and the Square class derives from the Shape class. A class can prevent other classes from inheriting from it by using the NotInheritable modifier on the class declaration.

Inheritance is most useful when multiple classes are defined that all share common traits. This allows code to be written that operates just on the common traits, which means the same routine can be used on many different classes.

 Class Shape   Public x, y As Integer   Sub Move(ByVal x As Integer, ByVal y As Integer)     Me.x = x     Me.y = y   End Sub End Class Class Square   Inherits Shape   Public Width, Height As Integer   ... End Class Class RightTriangle   Inherits Shape   Public SideLengths() As Integer   ... End Class Module Test   Sub Main()     Dim s As Square = New Square()     Dim t As RightTriangle = New RightTriangle()     s.Move(10, 20)     t.Move(30, 40)   End Sub End Module 

Because a derived class contains all the members of its base classes, an instance of a derived class can always be treated as an instance of a base class. For example, the following code declares a method that works on instances of Shape . Instances of Square can be passed to the method because Square contains all the members that Shape does.

 Module Test   Sub PrintLocation(ByVal Shape As Shape)     Console.WriteLine(Shape.x & "," & Shape.y)   End Sub   Sub Main()     Dim s As Square = New Square()     PrintLocation(s)   End Sub End Module 

Protected Access

The Protected access level allows class members to be accessed only by more derived types. This is useful for members that contain internal state that can be used by more derived classes. In the following example, the Person class declares a field, SSN , that is Private just to the Person class, and a field, Password , that is accessible to any derived class.

 Class Person   Private SSN As String   Protected Password As String End Class Class Employee   Inherits Person   Sub New()     ' Error: SSN is private to Person     SSN = "123-45-7890"     ' OK: Password is protected and can be accessed     Password = "password"   End Sub End Class 

Access to a Protected member in a derived class must take place through an instance of that derived class. Without this rule, it would be possible to gain access to a Protected member of another type simply by deriving from a common base class.

 Class Person   Protected Password As String End Class Class Employee   Inherits Person End Class Class Guest   Inherits Person   Public Sub PrintEmployeePassword(ByVal e As Employee)     Console.WriteLine(e.Password)   End Sub End Class 

In this example, Guest cannot access Employee 's protected field Password ”it can only access the Password field of instances of Guest . Protected access can be combined with Friend access, in which case derived classes and types within the same assembly can access the member.

Overriding

A base class may wish to allow a derived class to provide a different implementation for a method defined in the base class. A base class can do this by declaring a method as overridable . This allows the derived class to override the implementation of that method. For example, the following code allows the method PrintCoordinates to be overridden. The derived type Square overrides the base class implementation so that it can print all four coordinates.

 Class Shape   Public x, y As Integer   Overridable Sub PrintCoordinates()     Console.Write(x & "," & y)   End Sub End Class Class Square   Inherits Shape   Public Width, Height As Integer   Overrides Sub PrintCoordinates()     Console.Write(x & "," & y)     Console.Write("")     Console.Write((x + Width) & "," & (y + Height))   End Sub End Class 

When a class overrides a method that it inherits from a base class, the overriding method will be called regardless of the stated type of an instance. For example, the following code prints 1,1 - 4,4 , not 1,1 , because Square 's implementation of PrintCoordinates is still called even though it is typed as a Shape .

 Module Test   Sub Main()     Dim Shape As Shape     Dim Square As Square = New Square()     Square.x = 1     Square.y = 1     Square.Width = 3     Square.Height = 3     Shape = Square     Shape.PrintCoordinates()   End Sub End Module 

If an overriding method wishes to use the base class's implementation, it can call the base implementation using the qualifier MyBase .

 Overrides Sub PrintCoordinates()   MyBase.PrintCoordinates()   Console.Write("")   Console.Write((x + Width) & "," & (y + Height)) End Sub 

A base class can also require that derived classes override a method by using the keyword MustOverride and providing no implementation for the method. A class that contains MustOverride methods cannot be created directly, because it contains methods that have no implementation ”only derived classes that provide an implementation for the MustOverride methods can be created. A class with MustOverride methods must use the keyword MustInherit , which means that the class cannot be created directly. In the following example, the MustInherit class Shape defines a MustOverride method, Paint , that derived classes must provide an implementation for.

 MustInherit Class Shape   Public x, y As Integer   MustOverride Sub Paint() End Class Class Square   Inherits Shape   Public Width, Height As Integer   Overrides Sub Paint()     ...   End Sub End Class 

Finally, when a derived class overrides a method, it can prevent any further derived class from overriding the method by adding the NotOverridable modifier to the declaration.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

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