I was reading the No Dumb Questions section on page 274 and I was wondering how to accomplish what is being said in the excerpt at the end. I assume the interface would have to be implemented in the Stinger or Proboscis class but if I understand the concept they are trying to teach, doing it this way would not ensure the implementation of the interface in the NectarStinger or NectarCollector classes, so in essence if you have a method that accepts an interface as a parameter, you would not be able to pass an instance of NectarStinger or NectarCollector to the method because they don't implement the interface. In essence all you are doing by putting private Proboscis proboscis = new Proboscis(); in you class is creating an instance of an object that implements the interface. This is rather confusing unless I'm not understanding the concept, can anyone shed some light on this?
Thanks
Below is the excerpt from page 274.
Can you think of ways that you could still avoid duplicating
code while using an interface? You could create a separate
class called Stinger or Proboscis to contain
the code that’s specific to stinging or collecting nectar.
NectarStinger and NectarCollector
could both create a private instance of Proboscis, and
any time they needed to collect nectar, they’d call its methods
and set its properties.
This post has been edited by -Taurus-: 03 December 2012 - 05:31 PM