Can someone explain MustOverride?

Can someone explain not what MustOverride does, but why use it? Output function?

I have two classes: the first (RoomFactory);

Public MustInherit Class RoomFactory : Inherits baseFactory Private _roomid As Integer = 0 Private _roomname as String = "" Public Sub New() End Sub Public Sub New(ByVal roomid As Integer, ByVal roomname As String) Me.RoomId = roomid Me.RoomName = roomname End Sub Public MustOverride Function CreateRoom(ByVal roomdetails As RoomFactory) As Integer Public MustOverride Function IsRoomAvailable(ByVal roomdetails as RoomFactory) As Boolean // .. properties removed for brevity .. // 

Second class (Room)

 Public Class Room : Inherits RoomFactory Public Function CreateRoom(ByVal roomdetails As RoomFactory) As Integer Return 0 End Function Public Function IsRoomAvailable(ByVal roomdetails As RoomFactory) As Boolean Return False End Function End Class 

Firstly, I think this is correct, but I would like some advice otherwise - performance, etc. But I guess the main question is: why use MustOverride?

Please excuse my ignorance here.

+6
source share
4 answers

This is so that you can provide general functionality in the base class, but force-derived classes themselves implement certain bits of functionality.

In your factory situation, I would suggest using an interface rather than an abstract class, but in other cases it makes sense. System.Text.Encoding is a good example of an abstract class like System.IO.Stream .

+10
source share

You would use Overrideable for a method that has a default implementation in the base class.
If there is no (reasonable) default implementation, use a Mustoverride .

+4
source share

I am not a VB.NET expert, but I did C # correctly. In C #, an abstract keyword is equivalent. It should be used when you want all classes derived from your RoomFactory class to implement some kind of behavior that you define as abstract.

Suppose in your example, if you want all the objects in the class created by inheriting from the RoomFactory class to return their size. You must create a mustoverride function that says ReturnSize in the Roomfactory, and any type of room that inherits from it must implement this function.

You can do the same with interfaces. But using this MustInherit class allows you to add some default properties to RoomFactory, which will be common in all rooms.

Hope this helps.

+3
source share

MustOverride indicates that a property or procedure is not implemented in this class and must be redefined in the derived class before using it.

+1
source share

All Articles