Using Interfaces to force the creation of methods
-
I am not sure if I am not understanding the use of interfaces or just going about this the wrong way. Any help would be appreciated. Here is the gist of what I am trying to do: I have a class - Dog - which implements a few basic methods such as tail-wagging and slipper chewing:
class Dog { void wagTail(){...}; void chewSlippers(){...}; }
Now I wish to create a bunch of types of dogs, so:class doberman : Dog {...) class labrador : Dog {...} class chihuaha : Dog {...} etc.
So far so good. Dobermans, labs and chihuahas can all wag their tails and chew slippers. But I also want dogs to be able to do a trick, so I thought I would force the programmer to create the doTrick method by making it an interface:interface IDog { void doTrick(); }
Here is my problem. The programmer is going to deal with the instances of dogs as part of a collection, so he is not necessarily going to know if the object being used is a doberman or a labrador or what. e.g. the code may look like:foreach ( Dog myDog in AllDogsInMyHouse ) {...};
If I implement the interface on the individual classesclass doberman : Dog, IDog {...) class labrador : Dog, IDog {...} class chihuaha : Dog, IDog {...} etc.
the programmer must create the doTrick() method for each one - great. But the programmer isn't able to call myDog.doTrick() because doTrick is not defined for the class myDog. If, instead, I implement the interface on the class myDogclass Dog : IDog { void wagTail(){...}; void chewSlippers(){...}; void doTrick(){...}; }
the programmer can call myDog.doTrick(), but I now have to provide a default method for doTrick() which is automatically inherited by doberman, labrador, etc. This defeats the purpose - the programmer is not forced to create a doTrick() himself. Is there a way for me to accomplish both goals?Clive Pottinger Victoria, BC
-
I am not sure if I am not understanding the use of interfaces or just going about this the wrong way. Any help would be appreciated. Here is the gist of what I am trying to do: I have a class - Dog - which implements a few basic methods such as tail-wagging and slipper chewing:
class Dog { void wagTail(){...}; void chewSlippers(){...}; }
Now I wish to create a bunch of types of dogs, so:class doberman : Dog {...) class labrador : Dog {...} class chihuaha : Dog {...} etc.
So far so good. Dobermans, labs and chihuahas can all wag their tails and chew slippers. But I also want dogs to be able to do a trick, so I thought I would force the programmer to create the doTrick method by making it an interface:interface IDog { void doTrick(); }
Here is my problem. The programmer is going to deal with the instances of dogs as part of a collection, so he is not necessarily going to know if the object being used is a doberman or a labrador or what. e.g. the code may look like:foreach ( Dog myDog in AllDogsInMyHouse ) {...};
If I implement the interface on the individual classesclass doberman : Dog, IDog {...) class labrador : Dog, IDog {...} class chihuaha : Dog, IDog {...} etc.
the programmer must create the doTrick() method for each one - great. But the programmer isn't able to call myDog.doTrick() because doTrick is not defined for the class myDog. If, instead, I implement the interface on the class myDogclass Dog : IDog { void wagTail(){...}; void chewSlippers(){...}; void doTrick(){...}; }
the programmer can call myDog.doTrick(), but I now have to provide a default method for doTrick() which is automatically inherited by doberman, labrador, etc. This defeats the purpose - the programmer is not forced to create a doTrick() himself. Is there a way for me to accomplish both goals?Clive Pottinger Victoria, BC
-
Make the class Dog abstract, so you will have the following:
abstract class Dog : IDog {
void wagTail() { ... }
void chewSlippers() { ... }
abstract void doTrick();
}Hope this helps,
Sounds like somebody's got a case of the Mondays -Jeff
Many thanks. That worked. I had read about abstract, but it didn't quite make sense to me until now.
Clive Pottinger Victoria, BC
-
I am not sure if I am not understanding the use of interfaces or just going about this the wrong way. Any help would be appreciated. Here is the gist of what I am trying to do: I have a class - Dog - which implements a few basic methods such as tail-wagging and slipper chewing:
class Dog { void wagTail(){...}; void chewSlippers(){...}; }
Now I wish to create a bunch of types of dogs, so:class doberman : Dog {...) class labrador : Dog {...} class chihuaha : Dog {...} etc.
So far so good. Dobermans, labs and chihuahas can all wag their tails and chew slippers. But I also want dogs to be able to do a trick, so I thought I would force the programmer to create the doTrick method by making it an interface:interface IDog { void doTrick(); }
Here is my problem. The programmer is going to deal with the instances of dogs as part of a collection, so he is not necessarily going to know if the object being used is a doberman or a labrador or what. e.g. the code may look like:foreach ( Dog myDog in AllDogsInMyHouse ) {...};
If I implement the interface on the individual classesclass doberman : Dog, IDog {...) class labrador : Dog, IDog {...} class chihuaha : Dog, IDog {...} etc.
the programmer must create the doTrick() method for each one - great. But the programmer isn't able to call myDog.doTrick() because doTrick is not defined for the class myDog. If, instead, I implement the interface on the class myDogclass Dog : IDog { void wagTail(){...}; void chewSlippers(){...}; void doTrick(){...}; }
the programmer can call myDog.doTrick(), but I now have to provide a default method for doTrick() which is automatically inherited by doberman, labrador, etc. This defeats the purpose - the programmer is not forced to create a doTrick() himself. Is there a way for me to accomplish both goals?Clive Pottinger Victoria, BC
What Jeff said is the right way to do it; the Dog class should be abstract, and there should be an abstract method called DoTrick on it. Another way of doing this, if you wanted to keep the Dog class non-abstract, you could do it like this:
class Dog
{
public virtual void DoTrick()
{
// Does nothing!
}
}Notice the virtual method - that means derived classes, such as Dobermans, can override that method and provide their own implementation:
public class Doberman
{
public override void DoTrick()
{
// do a trick that only dobermans can do!
...
}
}This will accomplish the same thing: all dogs can do a trick. However, it doesn't force derivatives to implement DoTrick; if Doberman didn't override it, DoTrick would exist but would do nothing (e.g. see the Dog.DoTrick method). If you want to force derivatives to provide a custom DoTrick method, an abstract method is the way to go, just like Jeff showed you.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Story of the Spoiled Child The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I am not sure if I am not understanding the use of interfaces or just going about this the wrong way. Any help would be appreciated. Here is the gist of what I am trying to do: I have a class - Dog - which implements a few basic methods such as tail-wagging and slipper chewing:
class Dog { void wagTail(){...}; void chewSlippers(){...}; }
Now I wish to create a bunch of types of dogs, so:class doberman : Dog {...) class labrador : Dog {...} class chihuaha : Dog {...} etc.
So far so good. Dobermans, labs and chihuahas can all wag their tails and chew slippers. But I also want dogs to be able to do a trick, so I thought I would force the programmer to create the doTrick method by making it an interface:interface IDog { void doTrick(); }
Here is my problem. The programmer is going to deal with the instances of dogs as part of a collection, so he is not necessarily going to know if the object being used is a doberman or a labrador or what. e.g. the code may look like:foreach ( Dog myDog in AllDogsInMyHouse ) {...};
If I implement the interface on the individual classesclass doberman : Dog, IDog {...) class labrador : Dog, IDog {...} class chihuaha : Dog, IDog {...} etc.
the programmer must create the doTrick() method for each one - great. But the programmer isn't able to call myDog.doTrick() because doTrick is not defined for the class myDog. If, instead, I implement the interface on the class myDogclass Dog : IDog { void wagTail(){...}; void chewSlippers(){...}; void doTrick(){...}; }
the programmer can call myDog.doTrick(), but I now have to provide a default method for doTrick() which is automatically inherited by doberman, labrador, etc. This defeats the purpose - the programmer is not forced to create a doTrick() himself. Is there a way for me to accomplish both goals?Clive Pottinger Victoria, BC
While the above answers are correct, I'm going to throw a one into the pot that you might not be aware of. You can use Partial Methods to accomplish a similar thing in .NET 3.5. Basically it allows you to put a call to a partial method and call it from your code. If the method is present, in the implementation then it will be called otherwise it will be compiled out. Take a look at this[^] site for more information.
Deja View - the feeling that you've seen this post before.