Interfaces
Can have no code, un-creatable – just defines the sub names & variable names etc.
Interface Basics:
1) Define only the property and method signatures, NOT the actual implementation.
2) require that classes which implement the given Interface must adhere to these property and method signatures.
3) ARE LIKE CONTRACTS ... classes which implement an interface guarantee that they can be trusted to be used polymorphic-ly. If you need to change the way ‘’ a class works (i.e., change its property or method signatures) then you must MUST keep the old ones AND implement new properties and methods. One way to do this is to implement additional Interfaces.
4) Notice: No scope identifiers ... by default, it is scoped as 'Friend'. If we add a 'Public' in front of Interface IProduct', then we could scope the individual properties and methods as 'Public'.
5) Have other scoping rules ... when inside classes, etc. See help about Interfaces.
Example
Public Interface IProduct
Property SKU() As String
Property Name() As String
Property ListPrice() As Double
Function CalculateTax(ByVal country As String) As
Double
Function InStock() As Integer
End Interface
' Classes that implement Interfaces ...
'
1) Are agreeing to a contract ... this contract must not be broken. The ramifications of breaking a contract (i.e., changing the property and method signatures) might break older programs which rely on the old Interface.
2) Classes that implement a given Interface can extend the properties and methods, if desired ... and can implement additional Interfaces.
3) Classes can expose Interfaces (this too is a topic for another day.)
Public Class Book
Implements IProduct
Private m_SKU As String
Private m_Name As String
Private m_ListPrice As Double
Property SKU() As String Implements IProduct.SKU
Get
Return m_SKU
End Get
Set(ByVal Value As String)
m_SKU = Value
End Set
End Property
Property Name() As String Implements IProduct.Name
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
Property ListPrice() As Double Implements
IProduct.ListPrice
Get
Return m_ListPrice
End Get
Set(ByVal Value As Double)
m_ListPrice = Value
End Set
End Property
Function CalculateTax(ByVal country As String) As Double Implements IProduct.CalculateTax
Return 5
End Function
Function InStock() As Integer Implements IProduct.InStock
Return 8
End Function
End Class
A class can implement multiple interfaces.
Implements IClassA, IClassB
We will also need to use the implements on each property and method. For example
Function CalculateTax(ByVal country As String) As Double Implements IProduct.CalculateTax
-
Interfaces
Can have no code, un-creatable – just defines the sub names & variable names etc.
Interface Basics:
1) Define only the property and method signatures, NOT the actual implementation.
2) require that classes which implement the given Interface must adhere to these property and method signatures.
3) ARE LIKE CONTRACTS ... classes which implement an interface guarantee that they can be trusted to be used polymorphic-ly. If you need to change the way ‘’ a class works (i.e., change its property or method signatures) then you must MUST keep the old ones AND implement new properties and methods. One way to do this is to implement additional Interfaces.
4) Notice: No scope identifiers ... by default, it is scoped as 'Friend'. If we add a 'Public' in front of Interface IProduct', then we could scope the individual properties and methods as 'Public'.
5) Have other scoping rules ... when inside classes, etc. See help about Interfaces.
Example
Public Interface IProduct
Property SKU() As String
Property Name() As String
Property ListPrice() As Double
Function CalculateTax(ByVal country As String) As
Double
Function InStock() As Integer
End Interface
' Classes that implement Interfaces ...
'
1) Are agreeing to a contract ... this contract must not be broken. The ramifications of breaking a contract (i.e., changing the property and method signatures) might break older programs which rely on the old Interface.
2) Classes that implement a given Interface can extend the properties and methods, if desired ... and can implement additional Interfaces.
3) Classes can expose Interfaces (this too is a topic for another day.)
Public Class Book
Implements IProduct
Private m_SKU As String
Private m_Name As String
Private m_ListPrice As Double
Property SKU() As String Implements IProduct.SKU
Get
Return m_SKU
End Get
Set(ByVal Value As String)
m_SKU = Value
End Set
End Property
Property Name() As String Implements IProduct.Name
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
Property ListPrice() As Double Implements
IProduct.ListPrice
Get
Return m_ListPrice
End Get
Set(ByVal Value As Double)
m_ListPrice = Value
End Set
End Property
Function CalculateTax(ByVal country As String) As Double Implements IProduct.CalculateTax
Return 5
End Function
Function InStock() As Integer Implements IProduct.InStock
Return 8
End Function
End Class
A class can implement multiple interfaces.
Implements IClassA, IClassB
We will also need to use the implements on each property and method. For example
Function CalculateTax(ByVal country As String) As Double Implements IProduct.CalculateTax0Add a comment
Add a comment