Programming to an Interface



Hi,

I have just been reading about the concept of programming to an
interface. As I understand, it is useful to program to an interface
with respect to polymorpism. I.e. If you wanted to treat books and
videos in the same way, as a product for example (something that has a
price - for simplicity )you can make those objects implement the
interface and then program to that interface. So rather than create
code that calls, lets say...

book1.GetPrice();
'book1 being an implementation of the book class'

and
video1.GetPrice()
'video1 being an implementation of the video class'

You could instead write one piece of code that would call the correct
GetPrice method by means of polymorphism.

E.g.
===
product1.GetPrice()
'this would call the GetPrice for a video or book depending on the
object being dealt with'.

That's fine. I understand that. What does confuse me however, is the
notion that programming to an interface reduces the likelihood that
changing an object's implementation will break existing code (code
created by the client of the object (that implements the interface).
Why is this...?

For example, if you had the method AddTwoNumbers(numberOne, numberTwo)
in either an interface or in an object, it doesn't matter which you
use, you cannot change that method's signature (in either the interface
or an object) as doing so would break the client of the method (whether
it is called via an interface or via an object).

So where did this notion of reducing the chance of breaking client code
come from???

Surely, rather than add an additional method to an interface every time
a new method signature is required, you could simply add a new method
to the object... I just can't see how programming to an interface will
reduce the likelihood of breaking a clients code.

I'd really appreciate if someone could tell me where I'm thinking along
the wrong lines.

Many thanks in advance.

Craig.

.



Relevant Pages

  • Re: home made home security
    ... to interface to the RS485 master circuit. ... You do understand that the "master circuit" in this case needs to be a PC, ... What is the value of your time spent programming that you would ... Home security automation systems _don't_ cost thousands of dollars if you ...
    (comp.home.automation)
  • Re: Good book on OOP? HELP!
    ... > honest good book that can give me some good fundamentals about programming ... Arrays are an easier way to handle lots of data, and data structures are ... A class is the pattern for an object, and specifies all the details exactly, ... will not break existing systems if you use the same interface. ...
    (borland.public.delphi.non-technical)
  • Re: [patch 00/13] Syslets, "Threadlets", generic AIO support, v3
    ... streaming interface. ... Eventually it will be reading some blocks from the disk ... state machine, but in reality it would just become a "user-level thread ... In contrast, if you start using thread-like programming to begin with, you ...
    (Linux-Kernel)
  • Re: Good Dungeon Mapping e-Tool?
    ... for that, it doesn't extend well to other resources, and isn't ... of late to add it to every programming environment that gets created. ... explicit dispose call even though the GC would clean up the purely ... whether the interface that you're using points to a disposable object ...
    (rec.games.frp.dnd)
  • Re: Beginning Robotics
    ... interface to a microcontroller a programming cable, ... will also interface sonar, servos, an LCD display, and do interchip ... My goal is that in a few years to be entering robots into ...
    (comp.robotics.misc)