473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Why should I use interface?

Cannot see any good reason for using it (just duplication of code.).
Can you?

MH
Nov 15 '05 #1
2 3314
To gurantee that there is certain methods implemented ie., an interface.

IEnumerable is an interface, with out it you would not have Foreach loop
constructs.

"Marius Horak" <so*****@europe.eu> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Cannot see any good reason for using it (just duplication of code.).
Can you?

MH

Nov 15 '05 #2
"Marius Horak" <so*****@europe.eu> wrote in
news:Oe**************@tk2msftngp13.phx.gbl:
Cannot see any good reason for using it (just duplication of
code.). Can you?


Marius,

I'm sure you've familiar with object inheritance, sometimes known as
"vertical" inheritance. On a taxonomy chart of classes, a child
class descends from a parent class. That means the child class
appears below the parent class on the chart, hence the "vertical"
inheritance.

Interfaces allow for "horizontal" inheritance. Two or more classes
which do not have a vertical relationship may be related via
interfaces.

Here's a good example of the usefulness of interfaces. Suppose you
need to write a single method that will return a data reader for
either an OleDB data source (OleDbDataReader), or an MS SQL Server
data source (SqlDataReader).

Note that neither of these objects have a useful common "data
reader"-type parent. They both descend from MarshalByRefObject,
which is too generic to be useful here as a return type.

So we can't have the method's signature look like any of these:

public OleDbDataReader GetDataReader()
public SqlDataReader GetDataReader()
public MarshalByRefObject GetDataReader()

The last one would almost work, but the caller would have to downcast
to either OleDbDataReader or SqlDataReader. The problem is the
caller would not know which one to downcast to!

The solution is to use the IDataReader interface. Even
though OleDbDataReader and SqlDataReader aren't related "vertically",
the are related "horizontally" because they both implement the
IDataReader interface. The method signature becomes:

public IDataReader GetDataReader()

The caller can use the method like this:

IDataReader dr = GetDataReader();

while (dr.Read())
{
// Process rows here.
}

GetDataReader() could return either a OleDbDataReader or
SqlDataReader. The caller's code stays the same since it only cares
about the returned interface, and not the returned object's type.

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
2
by: Alex Sedow | last post by:
Why interface-event-declaration does not support multiple declarators like event-declaration? Grammar from C# spec: variable-declarators: variable-declarator variable-declarators ","...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.