I like the idea of an ITNERFACE that provides.. more capabilities. (or less).
HTMLDocumentClass htmlDoc = new HTMLDocumentClass();
IHTMLDocument2 iHTML = htmlDoc ;
Because HTMLDocumentClass IMPLEMENTS IHTMLDocument2, we can look at the HTMLDocumentClass object AS IF IT WERE JUST an object AS DESCRIBED BY the IHTMLDocument2 interface.
The INTERFACE is like a JOB.
ME. I can implement: ISoftwareProgrammer, IMan, IHusband, IBoxer, IMotorcyleRider.
Say I implement all those interfaces. I am an instance of class Human, implementing the above listed interfaces.
In your interaction with me, your interaction with me is LIMITED. So, say you are a woman who takes no sexual interest in me whatsoever. So you will basically view me from my PROFESSIONAL INTERFACE: my ISoftwareProgrammer interface. Your view of me is going to only be .. seeing me as a SoftwareProgrammer, and not as a boxer, husband or anything else that I AM. Because you dael with me THROUGH the ISoftwareProgrammger interface, you SEE ME AS just someone who implements the ISoftwareProgrammer interface requirements and you WILL NOT SEE any of the other interfaces that I implement.
In other words, dealing with me through an INTERFACE makes me … a pawn.
Dealing with people through the correct interface insures correct behavior. Knowing which interface to use, prevents lawsuits such as using your IBarAttendee interface on a coworker.
Because I am a Boxer, I COULD punch your lights out. But when you deal with me professionally, you actually won’t see that, because you deal with me exclusively through my ISoftwareProgrammer interface.
Now when I’m down at the track, riding my motorcycle, all the people THERE are seeing me as implementing the IMotorcycleRider interface. They don’t know and they don’t care what other interfaces I implement. I implement, IMan, IHusband and ISoftwareProgrammer interfaces, so they COULD interact with me through those interfaces .. through my softwareProgrammer interface I can be asked to .Code something – ( (ISoftwareProgrammer)me ).code() ;
REALLY, I have these methods:
me.Eat() // due to me being of base class Human
me.Sleep() // due to me being of base class Human
me.Code() // due to software programmer interface, i implement .code()
me.Grunt() // due to man interface, i implement .grunt()
me.Serve() // due to the husband interface, I implement .serve()
me.Punch() // due to IBoxer
But when you deal with me through the ISoftwareProgrammer interface, you actually do not see my other capabilities, really. If you dealth with me through the Human base class interface, you might see I only do .Eat() and .Sleep().
Doesn’t that make sense?
So, WHY program to an interface.
Programming to an interface makes it so that the way to interact with the underlying code remains fixed, even though the code itself might change radically.
To understand this, say you get work done on your car. You want to soup it up, you want to make it go faster. What kind of mods done to your car will _not bother you_? Mods that change / improve how the car works inside, WITHOUT CHANGING THE WAY YOU AS THE DRIVER INTERFACE TO THE CAR.
The interface of your car is standard. It has pedals to stop and go, and a wheel to steer. If you did a soup up that changed your steering wheel to a stick, then you WOULD have SERIOUS problems getting used to the new steering “wheel” (or stick). If suddenly the pedals were put on the roof, it would be very hard for you to get used to that. In fact, you would probably be angry.
Is it any wonder software programmers get angry when libraries change their interfaces? When a programming library changes its interface (ALA D3D, DirectX SDK is ALWAYS doing this) then it means that we as programmers must change our code that uses those interfaces. Microsoft has gone and made a wheel a stick, or a stick a wheel, and our program code must change to accomodate that fact.
Ideally interfaces are CONCRETE and do not change often. When an interface does NOT change, it means your code that uses an interface has a WAY easier time handling “upgrades”. Its like dropping a new engine into your car, and you finding the car equally easy to drive. The interface – or how you interact with the car – has not changed – the steering wheel and pedals are still the same. But something internal to the car – the engine in this case – has been improved and replaced – so while you still use your car the same way, it just works better.
In an ideal world that is what Interfaces are SUPPOSED to do for software. An Interface is SUPPOSED TO be written in stone, something that doesn’t change, and it is the “outline” of functions and data members that a class exposes for YOUR CODE to use. Upgrading the code of a class that IMPLEMENTS an interface should not require changes to the code that USES that class.
you can’t create an instance of an interface directly because.. well you can’t just have a SOFTWARE PROGRAMMER. There is no such thing.. there’s only people who are software programmers.. PEOPLE that IMPLEMENT the ISoftwareProgrammer interface. Invariably you’re going to get a PERSON who can do many more things than just software program.