Sometimes when you extend a base class, you may want to have a method that has the same name as the one in the base class but does something different. In VB.NET you can do this by using the keyword
Overridable in front of the declaration of the method in the base class. Then, in the derived class, you use the keyword
Overrides in front of the method with the same name. This technique is called method overriding.
For example, Listing 16 shows a class called
Employee with a method called
Work. The method is declared overridable, meaning that the method can be overridden in the inheriting classes.
Listing 16: An overridable method
Imports System Class Employee Overridable Public Sub Work() Console.Write("I am an employee.") End Sub End Class
The code in Listing 17 is a class called
Manager that extends the class
Employee in Listing 16. In the
Manager class, we provide a different implementation for the method
Listing 17: Method overriding
Class Manager: Inherits Employee Overrides Public Sub Work() Console.Write("I am a manager.") End Sub End Class
You can make a method not overridable by using the keyword
NotOverridable. For example, the
Work method in the
Employee class in Listing 18 cannot be overridden.
Listing 18: Not overridable method
Imports System Class Employee NotOverridable Public Sub Work() Console.Write("I am an employee.") End Sub End Class
In some cases, you don't know how to write the implementation of a method in a class at the time of writing the class. Implementation can only be provided by the inheriting class. Or, you know that the method will be overridden in the child classes, so why bother providing an implementation at all? If this is the case, your class is incomplete; this is called an abstract class. The method that you don't provide implementation for is called an abstract method. An abstract method is denoted by the
MustOverride keyword. The class itself has the
MustInherit modifier because it cannot be instantiated using the
For example, the class in Listing 19 is named
Shape and it is abstract because one of the methods (
Draw) does not have implementation. Other than that, the class looks like a normal class.
Listing 19: An abstract class
Imports System MustInherit Class Shape Public x As Integer = 9 Public y As Integer = 0 MustOverride Sub Draw() Public Function GetInfo() As String GetInfo = "An abstract class" End Function End Class
Note that the
Draw method does not have the
End Sub declaration.
When you extend an abstract class, you must provide implementations for all methods that must be overridden. Otherwise, the inheriting class itself will be an abstract class and must be declared with the
Line classes in Listing 20 inherit
Listing 20: Inheriting from an abstract class
Class Rectangle: Inherits Shape Overrides Sub Draw() ' Draw a rectangle here End Sub End Class Class Line: Inherits Shape Overrides Sub Draw() ' Draw a line here End Sub End Class
For beginners, interfaces are probably not very clear. Why would you want to use an interface at all? An interface defines a contract that classes must adhere to.
An interface is like an abstract class. An abstract class can have methods with or without implementations; however, an interface can only have methods that don't have implementation. In an OOP language that supports only single class inheritance like VB.NET, inheritance plays a very important role. Interfaces enable multiple inheritance in VB.NET because a VB.NET class can extend multiple interfaces. Extending an interface has a special term: implement. To implement an interface in VB.NET, you use the keyword
Implements. As with extending an abstract class, you must provide implementations for all methods in the interface you implement. Otherwise, you must make your class abstract.
An interface is declared using the keyword
Interface. Like other types in VB.NET, there is a naming convention for interfaces. The recommendation is to use the class's naming convention and prefix the interface name with an I. For example, here are some good names for interfaces:
For example, Listing 21 presents an interface called IShape.
Listing 21: An interface called IShape
Interface IShape End Interface
For a class to implement an interface, the same syntax as class extension is used. In the code in Listing 22, the class
Line implements the
Listing 22: Implementing an interface
Interface IShape End Interface Class Line: Implements IShape End Class
An alternative syntax would be:
Interface IShape End Interface Class Line Implements IShape End Class
An interface in VB.NET can only have methods, properties and events. An interface cannot contain a field. All interface members are implicitly public and may not specify an access modifier. In the class that implements an interface, each method implementation must specify the interface method that it implements using the keyword
Implements, followed by the name of the interface and the method name. The interface name and the method name are separated using a period.
For example, the code in Listing 23 shows an interface called
IShape with one method called
Draw. There is also a class named
Line that implements
Listing 23: Implementing an interface method
Imports System Interface IShape Sub Draw() End Interface Class Line Implements IShape Sub Draw() Implements IShape.Draw Console.Write("Draw a line") End Sub End Class
You can then instantiate a
Line object as you would a normal class. Like abstract classes, interfaces cannot be instantiated.