Inheritance Question

seahawk

New member
Joined
Nov 2, 2016
Messages
1
Programming Experience
10+
I have a question on VB.Net Inheritance. I am building a security pricing engine. Suppose I have a base class and multiple sub-classes. Each subclass has its own pricing methodology but the sensitivity calculation is all the same among all the subclasses. I am having a hard time to write the code since the "RateSensitivity" as an example should be in the common class but the price calculation is in all the subclasses. How should I structure the classes?
Public Class Common
        Public Maturity as double 
        
        Public Function RateSensitivity(Market as double)
                RateSensitivity = (Price(Market+1) +Price(Market-1))/2
        End Function
End Class

Public Class SecurityA
        inherits Common
        Public Maturity as double 
        '.....(different kinds of attributes)

        Public Function Price(Market as double)
              Price = Market^2
        End function
        
End Class
Public Class SecurityB
        inherits Common
        Public Maturity as double 
        '.....(different kinds of attributes)

        Public Function Price(Market as double)
              Price = log(Market^2)
        End function
        
End Class
 
Last edited by a moderator:
If the base class can be instantiated declare the Price function Overridable and include default implementation. Use Overrides in derived classes if needed.
Otherwise declare the base class MustInherit and the Price function there MustOverride (no implementation). Use Overrides in all derived classes.
 
Back
Top