Hi.
I have two basic interface-related concepts that I need to have a better understanding of.
1) How do I use interfaces if I only want to use some of the interface methods in a given class? For example, my FriendlyCat class inherits from Cat and implements ICatSounds. ICatSounds exposes MakeSoftPurr() and MakeLoudPurr() and MakePlayfulMeow(). But, it also exposes MakeHiss() and MakeLowGrowl() - both of which I don't need for my FriendlyCat class.
When I try to implement only some of the methods exposed by the interface the compiler complains that the others (that I don't need) have not been implemented.
Is the answer to this that I must create an interface that only contains the methods that I want to expose? For example, from my CatSounds class, I would create IFriendlyCatSounds? If this is true, then what happens when I want to use the other methods in another situation? Do I need to create another custom-tailored interface? This doesn't seem like good design to me.
It seems like I should be able to create an interface with all of the relevant methods (ICatSounds) and then pick and choose which methods I am using based on the implementation (FriendlyCat).
2) My second question is pretty basic but still a point of confusion for me. When I implement the interface (using Shift + Alt + F10) I get the interface's methods with "throw new NotImplementedException();" in the body. What else do I need to be doing besides referencing the interface method that I want to expose in my class? I am sure this is a big conceptual oops, but similar to inheriting from a base class, I want to gain access to the methods exposed by the interface wihtout adding to or changing them. What is the compiler expecting me to implement?
-- EDIT --
I understand #1 now, thanks for your answers. But I still need further elaboration on #2. My initial understanding was that an interface was a reflection of a the fully designed methods of a given class. Is that wrong? So, if ICatSounds has MakeSoftPurr() and MakeLoudPurr(), then both of those functions exist in CatSounds and do what they imply. Then this:
public class FriendlyCat: Cat, ICatSounds
{
...
public void ICatSounds.MakeLoudPurr()
{
throw new NotImplementedException();
}
public void ICatSounds.MakeSoftPurr()
{
throw new NotImplementedException();
}
}
is really a reflection of of code that already exists so why am I implementing anything? Why can't I do something like:
FriendlyCat fcat = new FriendlyCat();
fcat.MakeSoftPurr();
If the answer is, as I assume it will be, that the method has no code and therefore will do nothing. Then, if I want these methods to behave exactly as the methods in the class for which the interface is named, what do I do?
Thanks again in advance...