473,549 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interfaces

Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and
got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--

Nov 15 '05 #1
19 4543
And another question...

Because SomeClass derives from the IComparer interface
if i use SomeClass as a base of another like...

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}
}

Would that above fail due to no Compare method as SomeClass is derived from
ICompare and it MUST implement Compare.. like..

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

The above would work.

Do i understand this correctly?

--

Duncan McNutt
Microsoft Product Deactivation Team
--
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--

Nov 15 '05 #2
Hi Duncan,

You do not derive from an interface, you implement it, there is a diference,
an interface is like a template, it has no implementation it only assure
that the classes that implement it have a predefined setof methods ( the
interface ) , when you derive from a class you extend that class, add new
functionality. you do not do that with an interface.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--

Nov 15 '05 #3
oh well yeah oops :D wasnt thinking *gets coffee*

So why inherit from ICompare when its just as easy to implement Compare
anyway and its derived.

--

Duncan McNutt
Microsoft Product Deactivation Team
--
"Peter Vidler" <pv*****@gawab. com> wrote in message
news:bj******** ********@newsfe p1-win.server.ntli .net...
Hi,
Would that above fail due to no Compare method as SomeClass is
derived from ICompare and it MUST implement Compare.. like..
No, because SomeNewClass will inherit the implementation from SomeClass.

I'm almost certain of it ;)

Pete

Nov 15 '05 #4
Yeah I understand an Interface is a contract, that must be implemented but I
see code that has implemented that interface but why would it need to when
it can just as easily implement it anyway and not have tim use ICompare in
the class definition.

Whats to stop me just coding a public int Compare(object x, object y) method
anyway without referring to IComapre?

--

Duncan McNutt
Microsoft Product Deactivation Team
--
"Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote in message
news:uL******** ******@tk2msftn gp13.phx.gbl...
Hi Duncan,

You do not derive from an interface, you implement it, there is a diference, an interface is like a template, it has no implementation it only assure
that the classes that implement it have a predefined setof methods ( the
interface ) , when you derive from a class you extend that class, add new
functionality. you do not do that with an interface.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare

and
got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--


Nov 15 '05 #5
Hi Duncan,
I would suggest you something, if you have this kind of doubt, try it !! ,
do not missunderstand me, I believe that the best way to learn is
experimenting, your code will not fail cause as I explained in the previous
post when you derive you are extending therefore the new class gets all the
parent's functionality.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:ek******** ******@TK2MSFTN GP10.phx.gbl...
And another question...

Because SomeClass derives from the IComparer interface
if i use SomeClass as a base of another like...

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}
}

Would that above fail due to no Compare method as SomeClass is derived from ICompare and it MUST implement Compare.. like..

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

The above would work.

Do i understand this correctly?

--

Duncan McNutt
Microsoft Product Deactivation Team
--
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare

and
got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--


Nov 15 '05 #6
Hi,
So why inherit from ICompare when its just as easy to implement
Compare anyway and its derived.


Because then you can use an object of that type wherever IComparer is asked
for (i.e. calls to methods, etc). Otherwise there is no way to tell if an
object supports the Compare method without having a reference to the exact
type of the object.

Pete
Nov 15 '05 #7
The key element is that the purpose of an interface is that you can write
your code to a generic "interface" ..

For example, say you have a Windows app that needs to connect to a
datasource. That datasource could be a real database, a remote web service
and also an offline file. But you want to abstract HOW the datasource gets
it's data. If you couldn't abstract it, you'd have to write 3 versions of
code mixed in with your main application - to do this.

but instead, you could write your application to use the IDataAccess
interface (this is an interface you write, it has Connect() Authenticate(),
etc)..

Then you have classes such as Database, WebService and OfflineAccess that
implement that interface.

So Interfaces aren't so much a small-scale time-saver, they make more and
more sense the larger the app gets - and the purpose is to abstract the
specific of how something gets done.

Using your examples, if you write your application to use the IComparer
interface:

IComparer objMyComparer = new SomeClass();

Since SomeClass implements IComparer - the above is valid. Imagine that you
could have several different KINDS of comparers - and you would only need to
write your new classes to implement that same IComparer interface.

hope that helps..

" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--

Nov 15 '05 #8
Yeah but why must that class implement ICompare when it can just as easily
make a public Compare (obj , obj) method and be the same.

--

Duncan McNutt
Microsoft Product Deactivation Team
--
"Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote in message
news:uL******** ******@tk2msftn gp13.phx.gbl...
Hi Duncan,

You do not derive from an interface, you implement it, there is a diference, an interface is like a template, it has no implementation it only assure
that the classes that implement it have a predefined setof methods ( the
interface ) , when you derive from a class you extend that class, add new
functionality. you do not do that with an interface.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare

and
got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--


Nov 15 '05 #9
yeah I know, i just wasnt thinking, caffine levels droped :D

But my question is, why implement ICompare when I can just as easily make
the method without reference to IComapre at all in the class.

What is the benifit.
--

Duncan McNutt
Microsoft Product Deactivation Team
--
"Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote in message
news:eA******** *****@TK2MSFTNG P10.phx.gbl...
Hi Duncan,
I would suggest you something, if you have this kind of doubt, try it !! , do not missunderstand me, I believe that the best way to learn is
experimenting, your code will not fail cause as I explained in the previous post when you derive you are extending therefore the new class gets all the parent's functionality.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:ek******** ******@TK2MSFTN GP10.phx.gbl...
And another question...

Because SomeClass derives from the IComparer interface
if i use SomeClass as a base of another like...

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}
}

Would that above fail due to no Compare method as SomeClass is derived

from
ICompare and it MUST implement Compare.. like..

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

The above would work.

Do i understand this correctly?

--

Duncan McNutt
Microsoft Product Deactivation Team
--
" Duncan McNutt .[FTSE]" <pi*******@127. 0.0.701> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving
ICompare and
got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--



Nov 15 '05 #10

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

Similar topics

1
2191
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason...
30
2478
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes...
8
1768
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface like you can do in a class). I'm obviously missing something... John
9
5185
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
18
1966
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller classes that each
22
2076
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the...
18
2373
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
5
2998
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting...
10
1983
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's implementation seems pretty flexible and straightforward. Thanks.
23
2186
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.