Overriding Interface Implementations

An implementing class is free to mark any or all of the methods that implement the interface as overridable. Derived classes can then override or provide new implementations. For example, a Document class might implement the IStorable interface and mark the Read() and Write( ) methods as overridable. The Document might Read( ) and Write( ) its contents to a File type. The developer might later derive new types from Document, such as perhaps a Note or EmailMessage type. While the Document class implements Read( ) and Write to save to a File, the Note class might implement Read( ) and Write( ) to read from and write to a database.

Example 8-5 strips down the complexity of the previous examples and illustrates overriding an interface implementation. In this example, you’ll derive a new class named Note from the Document class.

Document implements the IStorable-required Read( ) method as an overridable method, and Note overrides that implementation.


Notice that Document does not mark Write( ) as overridable. You’ll see the implications of this decision in the analysis section that follows the output.

The complete listing is shown in Example 8-5 and analyzed in detail following.

Example 8-5. Overriding an interface implementation

Option Strict On Imports Microsoft.VisualBasic Imports System Namespace OverridingInterfaces Interface IStorable Sub Read( ) Sub Write( ) End Interface ' simplify Document to implement only IStorable Public Class Document ...

Get Programming Visual Basic .NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.