stu nicholls dot com | menu - Professional dropdown #3

click here


Interfaces

Creating Interfaces

Although class can inherit only from one base class, it can implement multiple interfaces. An interface is a specification for a set of class members but not an implementation.
In the following example Interface named person is created and the implementation is done in the class employee.


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim e1 As New employee()
e1.setname("Mr. Soklay")
TextBox1.Text = " Employee: " & e1.getname()
End Sub
End Class
Public Interface person
Sub setname(ByVal PersonName As String)
Function getname() As String
End Interface
Public Class employee
Implements person
Dim name As String
Sub setname(ByVal PersonName As String) Implements
person.setname
name = PersonName
End Sub
Function getname() As String Implements person.getname
Return name
End Function
End Class

Using Multiple Interface

VB.Net lets you to implement multiple interfaces at the same time, which is a dressed down version of multiple inheritance.
The following example shows that

Public Class Form1
Private Sub Button1_Click(ByVal sender As
System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim seyla As New employee()
seyla.setname("Seyla")
TextBox1.Text = " Employee " & seyla.getname()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim pesith As New vicepresident()
pesith.setname("pesith")
pesith.settitle("vice president")
TextBox1.Text = " Executive " & pesith.getname() & "," & pesith.gettitle()
End Sub
End Class
Public Interface person
Sub setname(ByVal PersonName As String)
Function getname() As String
End Interface
Public Class employee
Implements person
Dim name As String
Sub setname(ByVal PersonName As String) Implements person.setname
name = PersonName
End Sub
Function getname() As String Implements person.getname
Return name
End Function
End Class
Public Interface executive
Sub settitle(ByVal PersonName As String)
Function gettitle() As String
End Interface
Public Class WebDesigner Implements person, executive
Dim name , title As String
Sub settitle(ByVal executivetitle As String)Implements executive.settitle
title = executivetitle
End Sub
Function gettitle() As String Implements executive.gettitle
Return title
End Function
Sub setname(ByVal PersonName As String) Implements person.setname
name = PersonName
End Sub
Function getname() As String Implements person.getname
Return name
End Function
End Class

Overloading<< Previous   Next>>Polymorphism


comments powered by Disqus
click here


click here
Footer1