archanamanju
asked on
Interfaces
Hi,
Can anybody tell me wht is main purpose of interfaces in project?
Thanks
Archana
Can anybody tell me wht is main purpose of interfaces in project?
Thanks
Archana
homework
Are you referring to the use of Interfaces in VB.NET? If so, then it's purpose is to be able to handle objects, that are otherwise not related, in a polymorphic manner. For example, a tree, a file and a person are not related... you can't say that they inherit from a common base or from each other. However, they all have an age. So you can create an interface IAge that each class implements. Then you can retrieve Age for each object that implements the interface IAge.
Interfaces in VB and VB .NET as well in other Object oriented or object based (VB6) languages serve the same purpose - providing mechanism of accessing different objects in the same manner. Suppose you are writing an application that can store some data somewhere. A user of the application can choose will the app store the data in the text file or the database. You can do something like this : (VB 6 example)
'interface class - has empty methods since it doesn't actually do anything. Let's call it IStorage, and add a single method to it:
Public Sub Store (byval data as string)
'
End sub
Now we can create two new classes - CDBStorage and CFileStorage.
'CDBStorage
Implements IStorage
Private Sub IStorage_Store (byval RHS as string)
'here comes the actual code that writes data to the database
End sub
'CFileStorage
Implements IStorage
Private Sub IStorage_Store (byval RHS as string)
'here comes the actual code that writes data to a file
End sub
Now you can ask user does he wants to store data in the database or file, and do something like this:
Dim oStorage as IStorage
If bStoreToFile Then
Set oStorage = new CFileStorage
elseif bStoreToDB Then
Set oStorage = New CDBStorage
End if
oStorage.Store ("my data")
This might be over-simplified example - in the real world you'd have several methods of the IStorage class, but I hope you got it.
'interface class - has empty methods since it doesn't actually do anything. Let's call it IStorage, and add a single method to it:
Public Sub Store (byval data as string)
'
End sub
Now we can create two new classes - CDBStorage and CFileStorage.
'CDBStorage
Implements IStorage
Private Sub IStorage_Store (byval RHS as string)
'here comes the actual code that writes data to the database
End sub
'CFileStorage
Implements IStorage
Private Sub IStorage_Store (byval RHS as string)
'here comes the actual code that writes data to a file
End sub
Now you can ask user does he wants to store data in the database or file, and do something like this:
Dim oStorage as IStorage
If bStoreToFile Then
Set oStorage = new CFileStorage
elseif bStoreToDB Then
Set oStorage = New CDBStorage
End if
oStorage.Store ("my data")
This might be over-simplified example - in the real world you'd have several methods of the IStorage class, but I hope you got it.
ASKER
Sorry,This is not i was looking for...
actually i have been asked in 1 interview that ...
Could u pls tell me the main purpose of interface in Com components..?
Can anybody answer me this pls.
Thanks in Advance
Archana
actually i have been asked in 1 interview that ...
Could u pls tell me the main purpose of interface in Com components..?
Can anybody answer me this pls.
Thanks in Advance
Archana
Since COM components are subroutines and functions the interface specifies the required parameters and return values.
mlmcc
mlmcc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In general an interface is how information is passed from one object to another.
For instance the user interface allows the user to communicate information and actions to the program. (Keyboard, voice, mouse, etc)
Subroutines can also have an interface. This is the parameter list in the subroutine heading
sub PrintData (Name as string, address as string)
Calling routine simply passes the name and address to the print routine
PrintData MyName, MyAddress
mlmcc