-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Sitaraman
Message 2 in Discussion
Hi Ravi
Two ways of doing it. Using Method Overriding or Shadows. The sample Code is as
follows. I have 2 classes. "clsBase", which is the base class and "clsDerived" which
derived from "clsBase"
Public Class clsBase
Public Overridable Function add() As Boolean
'No Implementation. Only definition. Also uses the Overridable keyword where
the base
'class designer allows Derived Class Designer to override the method
End Function
Public Overridable Function update() As Boolean
'Implementation is there . But uses the Overridable keyword where the base
'class designer allows the Derived Class Designer to override the method
MsgBox("Base.Update Called")
End Function
Public Function delete() As Boolean
' Implemntation is there. Base Class Designer has not used overridable to
allow the
' Derived Class Designer to override the implementation
MsgBox("Base.Delete Called")
End Function
End Class
Public Class clsDerived
Inherits clsBase
Public Overrides Function add() As Boolean
' Derived Class Designer overrides and gives the implementation for the add
method
MsgBox("Derived.Add Called")
End Function
Public Overrides Function update() As Boolean
' Derived Class Designer overrides and gives the implementation for the update
method
MsgBox("Derived.Update Called")
End Function
Public Shadows Function delete() As Boolean
' Though the Base Class Designer has NOT used the overridable keyword to allow
the Derived
' Class designer to override the Delete method, still Derived Class Designer
can use the
' "Shadows" keyword to forcibly override the base class method
MsgBox("Derived.Update Called")
End Function
End Class Here Note that in the case of add and update method the Base Class
explicitly uses the overridable keyword to denote that the implementation can be
overridden by a derived class and Derived Class Designer overrides the methods using
the overrides keyword.
However, the Base Class Designer does not give you the option for the delete method
in "clsBase"(No overridable keyword for this method). In spite of it the Derived
class designer can use the Shadows keyword to forcibly override the delete function.
Interesting point is that does this violate the basic OO concepts(Not the first time i
would have seen such violations being done by MS to offer better development features
:) )
All you want to know about overrides and shadows is available at link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet12252001.asp
Now do note that in the sample code, overriding the base class methods is optional.
Overridable, as the name specifies just means that the Dervied class has the option of
overriding the method. If someone derives from base and does not override the methods
then there is no problem. There will be no error. What if you want to force every
derived class to DEFINITELY override these methods. Simple - just two additional steps
in the base
1) Attach the keyword MustInherit to the Class
2) Attach the keyword MustOverride to the method
Now if you dont override the method with the MustOverride Keyword in the Derived
Class, then you wil get a compilation error
The Code sample is as follows
Public MustInherit Class clsBase
Public Overridable Function add() As Boolean
'No Implementation. Only definition. Also uses the Overridable keyword where
the base
'class designer allows the Derived Class Designer to override the method
End Function
Public Overridable Function update() As Boolean
'Implementation is there . But uses the Overridable keyword where the base
'class designer allows the Derived Class Designer to override the method
MsgBox("Base.Update Called")
End Function
Public Function delete() As Boolean
' Implemntation is there. Base Class Designer has not used overridable to
allow the
' Derived Class Designer to override the implementation
MsgBox("Base.Delete Called")
End Function
Public MustOverride Function fnX()
' Note - No End Function fo this
End Class
Public Class clsDerived
Inherits clsBase
Public Overrides Function add() As Boolean
' Derived Class Designer overrides and gives the implementation for the add
method
MsgBox("Derived.Add Called")
End Function
Public Overrides Function update() As Boolean
' Derived Class Designer overrides and gives the implementation for the update
method
MsgBox("Derived.Update Called")
End Function
Public Shadows Function delete() As Boolean
' Though the Base Class Designer has NOT used the overridable keyword to allow
the Derived
' Class designer to override the Delete method, still Derived Class Designer
can use the
' "Shadows" keyword to forcibly override the base class method
MsgBox("Derived.Update Called")
End Function
Public overrides Function fnX() As Boolean
' If this method is not overridden in Derived, it will throw a compilcation
error
MsgBox("Derived.Update Called")
End Function
End Class
Hope this helps
regards,
sr
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]