Introduction to Interfaces
Introduction to Interfaces
Interfaces Fundamentals
Introduction
We described abstract classes as those intended for inheritance. Another type of class designed for this purpose is referred to as an interface. An interface is a class that creates a foundation that new derived classes can use. As done for an abstract class, you can create original behavior that the deriving classes would use.
Unlike abstract classes, you don't implement the members (properties and procedures) of the interface. The general idea of an interface is only to lay a foundation, as creating a structural base, that the new classes would follow, although they can customize the behavior(s) of the parent interface but the parent interface does not "decide" what the behavior of a member would be.
To create an interface, you start with the Interface keyword followed by the name of the interface. You end the interface definition with an End Interface line. By tradition or good habit, the name of an interface starts with I. Here is an example of a starting interface:
Public Interface ITriangle End Interface
After creating an interface, you can derive a class from it. When deriving from an interface, instead of the Inherits keyword, you use Implements followed by the name of the interface. Here is an example of a new class named Regular and that is based on the above ITriangle interface:
Public Interface ITriangle
End Interface
Public Class RegularTriangle
Implements ITriangle
End Class
As with other classes, once you have derived the class, you can create objects from it and instantiate it using the New operator. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim reg As RegularTriangle
reg = New RegularTriangle
Return 0
End Function
End Module
As mentioned earlier, the purpose of having an interface is to create a skeleton that derived classes would follow. To do this, in the body of the interface, you can create the necessary members that you want to make available to new classes. The members can be the same types of methods or properties as those we have used in classes so far. Here are examples:
Public Interface ITriangle
ReadOnly Property Name() As String
Property Base() As Double
Property Height() As Double
Function CalculatePerimeter() As Double
Function CalculateArea() As Double
End Interface
The primary rule you must observe when deriving a class from an interface is that you must implement each member of the interface in the derived class. If you omit implementing a member of the parent interface, you would receive an error. When implementing a member of the interface, it must be followed by the Implements keyword, the name of the interface, the period operator, and the name of the member that it is implementing. Based on this, our Regular class can implement the ITriangle interface as follows:
Public Interface ITriangle
ReadOnly Property Name() As String
Property Base() As Double
Property Height() As Double
Function CalculatePerimeter() As Double
Function CalculateArea() As Double
End Interface
Public Class RegularTriangle
Implements ITriangle
Public bas As Double
Public hgt As Double
Public sd1 As Double
Public sd2 As Double
' Default constructor: the user will specify the dimensions
Public Sub New()
bas = 0
hgt = 0
sd1 = 0
sd2 = 0
End Sub
' A triangle based on known base and height
Public Sub New(ByVal b As Double, ByVal h As Double)
bas = b
hgt = h
End Sub
' A triangle based on the measurements of the sides
Public Sub New(ByVal b As Double, ByVal side1 As Double, ByVal side2 As Double)
bas = b
sd1 = side1
sd2 = side2
End Sub
' A triangle whose all sides and the height are known
Public Sub New(ByVal b As Double, ByVal h As Double, _
ByVal side1 As Double, ByVal side2 As Double)
bas = b
hgt = h
sd1 = side1
sd2 = side2
End Sub
Public Property Base() As Double Implements ITriangle.Base
Get
Return bas
End Get
Set(ByVal Value As Double)
If bas < 0 Then
bas = 0
Else
bas = Value
End If
End Set
End Property
Public Function CalculateArea() As Double Implements ITriangle.CalculateArea
Return bas * hgt / 2
End Function
Public Function CalculatePerimeter() As Double Implements _
ITriangle.CalculatePerimeter
Return bas + sd1 + sd2
End Function
Public Property Height() As Double Implements ITriangle.Height
Get
Return hgt
End Get
Set(ByVal Value As Double)
If hgt < 0 Then
hgt = 0
Else
hgt = Value
End If
End Set
End Property
Public ReadOnly Property Name() As String Implements ITriangle.Name
Get
Return "Regular Triangle"
End Get
End Property
End Class
Once the class has been defined like this, you can then instantiate and use it. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim reg As RegularTriangle = New RegularTriangle(35.28, 26.44)
Console.WriteLine("Triangle Type: {0}", reg.Name)
Console.WriteLine("=-= Characteristics =-=")
Console.WriteLine("Base: {0}", reg.Base)
Console.WriteLine("Height: {0}", reg.Height)
Console.WriteLine("Area: {0}", reg.CalculateArea)
Return 0
End Function
End Module
This would produce:
Triangle Type: Regular Triangle =-= Characteristics =-= Base: 35.28 Height: 26.44 Area: 466.4016
In the same way, you can derive other classes from an interface. For example, from our ITriangle class, you can derive an isosceles, a right, or an equilateral triangle. Always remember that when you derive a class, you must implement all of the members of the interface. You can also add new members as you see fit.
An Interface Derived
Like a regular class, an interface can be derived from another interface but an interface cannot derive from a class. To create an interface based on another, use the Inherits keyword as we have used in other classes. Here is an example:
Public Interface IGeometricShape
End Interface
Public Interface ITriangle
Inherits IGeometricShape
End Interface
As mentioned for the interfaces, you can use the parent interface to list the members that the deriving classes would implement. Still remember that since an interface cannot implement a member, the member of the parent interface cannot be defined in a derived interface. This implement would wait for the actual class(es) that would be based on the child (or even the parent) interface. Here is an example:
Public Interface IGeometricShape
ReadOnly Property Type() As String
End Interface
Public Interface ITriangle
Inherits IGeometricShape
ReadOnly Property Name() As String
Property Base() As Double
Property Height() As Double
Function CalculatePerimeter() As Double
Function CalculateArea() As Double
End Interface
After deriving a class from an interface, when defining the class, you must implement the member of the immediate interface and those of the ancestor interface(s). Here is an example:
Public Interface IGeometricShape
ReadOnly Property Type() As String
End Interface
Public Interface ITriangle
Inherits IGeometricShape
ReadOnly Property Name() As String
Property Base() As Double
Property Height() As Double
Function CalculatePerimeter() As Double
Function CalculateArea() As Double
End Interface
Public Class RegularTriangle
Implements ITriangle
Public bas As Double
Public hgt As Double
Public sd1 As Double
Public sd2 As Double
Public ReadOnly Property Type() As String Implements ITriangle.type
Get
Return "Triangle"
End Get
End Property
' Default constructor: the user will specify the dimensions
Public Sub New()
bas = 0
hgt = 0
sd1 = 0
sd2 = 0
End Sub
' A triangle based on known base and height
Public Sub New(ByVal b As Double, ByVal h As Double)
bas = b
hgt = h
End Sub
' A triangle based on the measurements of the sides
Public Sub New(ByVal b As Double, ByVal side1 As Double, ByVal side2 As Double)
bas = b
sd1 = side1
sd2 = side2
End Sub
' A triangle whose all sides and the height are known
Public Sub New(ByVal b As Double, ByVal h As Double, _
ByVal side1 As Double, ByVal side2 As Double)
bas = b
hgt = h
sd1 = side1
sd2 = side2
End Sub
Public Property Base() As Double Implements ITriangle.Base
Get
Return bas
End Get
Set(ByVal Value As Double)
If bas < 0 Then
bas = 0
Else
bas = Value
End If
End Set
End Property
Public Function CalculateArea() As Double Implements ITriangle.CalculateArea
Return bas * hgt / 2
End Function
Public Function CalculatePerimeter() As Double Implements _
ITriangle.CalculatePerimeter
Return bas + sd1 + sd2
End Function
Public Property Height() As Double Implements ITriangle.Height
Get
Return hgt
End Get
Set(ByVal Value As Double)
If hgt < 0 Then
hgt = 0
Else
hgt = Value
End If
End Set
End Property
Public ReadOnly Property Name() As String Implements ITriangle.Name
Get
Return "Regular"
End Get
End Property
End Class
Here is an example of testing the class:
Module Exercise
Public Function Main() As Integer
Dim reg As RegularTriangle = New RegularTriangle(35.28, 26.44)
Console.WriteLine("Shape Type: {0}", reg.Type)
Console.WriteLine("Triangle Type: {0}", reg.Name)
Console.WriteLine("=-= Characteristics =-=")
Console.WriteLine("Base: {0}", reg.Base)
Console.WriteLine("Height: {0}", reg.Height)
Console.WriteLine("Area: {0}", reg.CalculateArea)
Return 0
End Function
End Module
This would produce:
Shape Type: Triangle Triangle Type: Regular =-= Characteristics =-= Base: 35.28 Height: 26.44 Area: 466.4016
|
Multiple Inheritance |
Multiple inheritance consists of creating a class that is based on more than one parent. In the Microsoft Visual Basic language (in fact in the .NET Framework), you cannot derive a class from more than one class. This functionality is available only with interfaces.
To create a class based on more than one interface, after the Implements keyword, enter the name of each interface and separate them with commas. Here is an example:
Public Interface IGeometricShape
ReadOnly Property Type() As String
End Interface
Public Interface ICalculation
End Interface
Public Interface ITriangle
Inherits IGeometricShape, ICalculation
End Interface
The same rules apply for multiple inheritance: you must implements all members of each parent interface.
Besides deriving from an interface, you can also create a class that is based on a class and one or more interfaces. To do this, under the line that specifies the name of the class, use the Inherits keyword to specify the name of the parent, press Enter, and use the Implements keyword to specify the name of the class that serves as the parent interface. Here is an example:
Public Interface IGeometricShape
ReadOnly Property Type() As String
End Interface
Public Interface ICalculation
End Interface
Public Class Geometry
End Class
Public Interface ITriangle
Inherits IGeometricShape, ICalculation
End Interface
Public Class RegularTriangle
Inherits Geometry
Implements ITriangle
End Class
In the same way, you can create a class that is based on more than one interface but it can be based on only one class.
|
Issues on Using Inherited Classes |
Public Class Employee
Private fn As String
Private ln As String
Public Property FirstName() As String
Get
Return fn
End Get
Set(ByVal value As String)
fn = value
End Set
End Property
Public Property LastName() As String
Get
Return ln
End Get
Set(ByVal value As String)
ln = value
End Set
End Property
Public Overrides Function ToString() As String
Return LastName & ", " & FirstName
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim empl As Employee
empl = TryCast(obj, Employee)
If Me.FirstName = empl.FirstName And Me.LastName = empl.LastName Then
Return True
Else
Return False
End If
End Function
End Class
Public Class Exercise
Private Function AreEqual(ByVal x As Integer, _
ByVal y As Integer) As Boolean
Return x = y
End Function
Private Function AreEqual(ByVal x As Object, ByVal y As Object) As Boolean
If TypeOf x Is Employee And TypeOf y Is Employee Then
Return x.Equals(y)
End If
End Function
Private Sub BtnProcess_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnProcess.Click
Dim empl1 As Employee
empl1 = New Employee
empl1.FirstName = "Patricia"
empl1.LastName = "Katts"
Dim empl2 As New Employee
With empl2
.FirstName = "Raymond" REM = "Patricia"
.LastName = "Kouma" REM = "Katts"
End With
MsgBox(empl1.ToString & " = " & empl2.ToString & ": " & AreEqual(empl1, empl2))
End Sub
End Class
|
|
|||
| Previous | Copyright © 2009-2026, FunctionX | Wednesday 10 December 2025, 12:21 | Next |
|
|
|||