Interfaces
-
Interfaces is a method to provide polymorphism without having to implement multiple inheritance. An interface is a description of a subset of properties and methods of an object. If an object is declared to implement an interface, then it must provide all the properties and methods of that interface. A good example of the usefullness of interfaces is IDisposable. Let's say that you got a list of objects. The objects are all of different types, but you know that some of those objects needs to release their internal resources when you're done using them. Here is how you could do it:
foreach (IDisposable disposableObject in listOfVariousClassInstances)
{
disposableObject.Dispose();
}In the example above, you don't care if the object at hand is a Brush, BinaryWriter or a Cursor, you simply narrows the object to an IDisposable and invokes the one and only method available: Dispose(). When to create an interface: If you expect that your class hierarchy contains classes that share common methods and properties, you will of course use inheritance:
public class Base
{
protected int i;public int I
{
get:
{
return i;
}
}
}public class A: Base
{
protected int j;public int J
{
get:
{
return j;
}
}
}public class B: Base
{
protected int k;public int K
{
get:
{
return k;
}
}
}But suppose that you want class B to be a list of objects too. All that functionality could be easily provided by extending class System.Collections.ArrayList, but then you can't inherit from class Base. You know that other classes in your hierarchy will refer to B.I, so you now have the choice to either reinvent the functionality of ArrayList, or create a common interface for your classes. In this case, it seems that your classes aren't that related, which in itself begs for an interface solution, but it's quite clear, that it should be easier to copy/paste the I property to all of your classes, than to reinvent ArrayList:
public interface IBase
{
int I
{
get;
}
}public class A: IBase
{
protected int j;
protected int i;public int I
{
get:
{
return i;
}
}public int J
{
get:
{
return j;
}
}
}public class B: ArrayList, IBase
{
protected int k;
protected int i;public int I
{Thanks a lot for the examples and the explanations.
-
Interfaces is a method to provide polymorphism without having to implement multiple inheritance. An interface is a description of a subset of properties and methods of an object. If an object is declared to implement an interface, then it must provide all the properties and methods of that interface. A good example of the usefullness of interfaces is IDisposable. Let's say that you got a list of objects. The objects are all of different types, but you know that some of those objects needs to release their internal resources when you're done using them. Here is how you could do it:
foreach (IDisposable disposableObject in listOfVariousClassInstances)
{
disposableObject.Dispose();
}In the example above, you don't care if the object at hand is a Brush, BinaryWriter or a Cursor, you simply narrows the object to an IDisposable and invokes the one and only method available: Dispose(). When to create an interface: If you expect that your class hierarchy contains classes that share common methods and properties, you will of course use inheritance:
public class Base
{
protected int i;public int I
{
get:
{
return i;
}
}
}public class A: Base
{
protected int j;public int J
{
get:
{
return j;
}
}
}public class B: Base
{
protected int k;public int K
{
get:
{
return k;
}
}
}But suppose that you want class B to be a list of objects too. All that functionality could be easily provided by extending class System.Collections.ArrayList, but then you can't inherit from class Base. You know that other classes in your hierarchy will refer to B.I, so you now have the choice to either reinvent the functionality of ArrayList, or create a common interface for your classes. In this case, it seems that your classes aren't that related, which in itself begs for an interface solution, but it's quite clear, that it should be easier to copy/paste the I property to all of your classes, than to reinvent ArrayList:
public interface IBase
{
int I
{
get;
}
}public class A: IBase
{
protected int j;
protected int i;public int I
{
get:
{
return i;
}
}public int J
{
get:
{
return j;
}
}
}public class B: ArrayList, IBase
{
protected int k;
protected int i;public int I
{jan larsen wrote: foreach (IDisposable disposableObject in listOfVariousClassInstances) { disposableObject.Dispose(); } That won't work if at least one item from listOfVariousClassInstances will NOT implement IDisposable, right? I guess you know it, bt I think that rookie may not know... so I say it :) foreach loop does type casting for every item in collection, so it will raise exception if some item from
listOfVariousClassInstances
collection can't by casted to IDisposable. correct me if I am wrong... As I do :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.) -
jan larsen wrote: foreach (IDisposable disposableObject in listOfVariousClassInstances) { disposableObject.Dispose(); } That won't work if at least one item from listOfVariousClassInstances will NOT implement IDisposable, right? I guess you know it, bt I think that rookie may not know... so I say it :) foreach loop does type casting for every item in collection, so it will raise exception if some item from
listOfVariousClassInstances
collection can't by casted to IDisposable. correct me if I am wrong... As I do :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)well "that rookie" knows about it... and I didnt like the kind of the way you pointing me.. I'm sorry, but you should be more polite...:~
-
well "that rookie" knows about it... and I didnt like the kind of the way you pointing me.. I'm sorry, but you should be more polite...:~
Bahadir Cambel wrote: well "that rookie" knows about it... OK. Don't call me only ignorant* but pesimistic ignorant from now. My theory is better say something obvious again than forget to say something critical. Bahadir Cambel wrote: I'm sorry, but you should be more polite.. you mean "that rookie" ? Sorry than.. I am so stupid I cannot remember who was author of original post as I see only post I am replying to... so I had to use something else than your name... well i could use "s/he" ... *click* another lesson learnt At the top of the things, I always had problems (nope, I never... others have problem wit me ) to be polite... and I am not going to change myself in this way much... sorry man that's the way I am. David *that's another, long story Never forget: "Stay kul and happy" (I.A.)
-
Bahadir Cambel wrote: well "that rookie" knows about it... OK. Don't call me only ignorant* but pesimistic ignorant from now. My theory is better say something obvious again than forget to say something critical. Bahadir Cambel wrote: I'm sorry, but you should be more polite.. you mean "that rookie" ? Sorry than.. I am so stupid I cannot remember who was author of original post as I see only post I am replying to... so I had to use something else than your name... well i could use "s/he" ... *click* another lesson learnt At the top of the things, I always had problems (nope, I never... others have problem wit me ) to be polite... and I am not going to change myself in this way much... sorry man that's the way I am. David *that's another, long story Never forget: "Stay kul and happy" (I.A.)
ok , the critical thing is not critical for me right now, and when I consider the example , it enlightened me a lot..So I didnt intend to write or answer about the IDispose.. I wont talk about about the politeness , thats your choice , and if you like the way ppl threaten to you , than everything is fine.. Whatever, I know your are trying to help ppl , the way you do..it is ok for me... Bahadir
-
ok , the critical thing is not critical for me right now, and when I consider the example , it enlightened me a lot..So I didnt intend to write or answer about the IDispose.. I wont talk about about the politeness , thats your choice , and if you like the way ppl threaten to you , than everything is fine.. Whatever, I know your are trying to help ppl , the way you do..it is ok for me... Bahadir
well, one more try... was it that "rookie" thing? :confused: my problem can be that I DONT see anything rude in that post :confused: Bahadir Cambel wrote: and if you like the way ppl threaten to you actually I don't. Most people don't like when i say what I think.. bt saying something else is something I detest. David Never forget: "Stay kul and happy" (I.A.)
-
jan larsen wrote: foreach (IDisposable disposableObject in listOfVariousClassInstances) { disposableObject.Dispose(); } That won't work if at least one item from listOfVariousClassInstances will NOT implement IDisposable, right? I guess you know it, bt I think that rookie may not know... so I say it :) foreach loop does type casting for every item in collection, so it will raise exception if some item from
listOfVariousClassInstances
collection can't by casted to IDisposable. correct me if I am wrong... As I do :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)Ah, yes, there is that. But all that will be corrected when we get generics... "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
-
Ah, yes, there is that. But all that will be corrected when we get generics... "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
true. Did you find my post unpolite? was "rookie" bad? :~ David Never forget: "Stay kul and happy" (I.A.)
-
true. Did you find my post unpolite? was "rookie" bad? :~ David Never forget: "Stay kul and happy" (I.A.)
Well, if he truly was a rookie, it wasn't bad. But he could be a very good C programmer that is about to learn OOP, in that case, he would probably find rookie a bit insulting :-) I was a consultant in a .NET project a couple of years ago (a very bold decision from a big company to use the brand new technology), where more than half of the staff were new to OOP, and they certainly weren't at home with GC. I used a lot of hours reeding out resource leaks by inserting dispose() invocations and encapsulating functionality inside
using(...)
blocks. I even had to expose my ears to statements like: "Hey, look what Bob did!, isn't it clever?, now we can use this code in blah blah blah", the guy was talking about 'bob' having implemented a class for Gods sake! But I never considered them rookies, at many points, some of them were much more experienced programmers, than I was. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus -
Well, if he truly was a rookie, it wasn't bad. But he could be a very good C programmer that is about to learn OOP, in that case, he would probably find rookie a bit insulting :-) I was a consultant in a .NET project a couple of years ago (a very bold decision from a big company to use the brand new technology), where more than half of the staff were new to OOP, and they certainly weren't at home with GC. I used a lot of hours reeding out resource leaks by inserting dispose() invocations and encapsulating functionality inside
using(...)
blocks. I even had to expose my ears to statements like: "Hey, look what Bob did!, isn't it clever?, now we can use this code in blah blah blah", the guy was talking about 'bob' having implemented a class for Gods sake! But I never considered them rookies, at many points, some of them were much more experienced programmers, than I was. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Grausjan larsen wrote: But he could be a very good C programmer that is about to learn OOP, in that case, he would probably find rookie a bit insulting Oh yeah... :-O I was thinking in contex of what he is learing... bt no one can know what I am thinking unless I write it down :doh: - my fault. I understand now... thx David Never forget: "Stay kul and happy" (I.A.)