The fighting continues...
The previous example about MyPatriot and MyTerrorist both implementing MyWarInterface is not wrong. They both share a common functionality which is to denotate the bomb. They don't need to have a similar objective per se. One can denotate the bomb in a room, one can denotate the bomb in the garden, another one in the garage. Who cares? Those classes detonate a bomb thus they should implement MyWarInterface.
Similar to the example above walk(). One class can walk south, one can walk north, another can walk west, and another towards east. They all share the same functionality walk.
Don't confuse interface with an
abstract class. If what your intention is that a dog, insect, monkey, chicken inherit a function walk() which means moving the legs one step after another, then you should use an abstract class or parent class say Animal. Then dog, insect, monkey, and chicken can inherit walk().
If your intention is to have
DIFFERENT implementations like walk north, walk south, east, or west, then you should be using an interface. They don't need to have a 'common theme' as what @yanong is indicating. He's probably confusing it with an abstract class that's why he keeps on using the word 'operation' so much.
Implementation and operation are two different things. What is important is that the signatures of your interface are implemented thus adhering to the contract.
In real-world practice, let's say your company is in the process of migrating from old systems so you have to build an application that will need to pull data from both a legacy database and from some web services published by some other systems... This is a typical scenario where data is scattered throughout multiple systems. Probably scalability was not anticipated.
Anyway, so you'll have a Repository interface with for e.g. a function
findByAddress(String address). Then you'll have two implementing classes
CustomerWS and
CustomerDAO. The former retrieves customers from a web service by address, and the latter from a database or mutliple databases through stored procedures by address. They both have different implementations but still complying to the contract.
You can also have another set of implementing class like
SupplierWS and
SupplierDAO and both implementing findByAddress(String address). Their objective is to retrieve suppliers information from both web services and legacy databases by address.
So going back to the MyPatriot and MyTerrorist example we can translate that into a fictional application that manages World War III. You have the MyWarInterface with several functionalities or signatures and one of them is detonateBomb(). You then have several implementing classes MyPatriot, MyTerrorist, and MySpy all implementing the detonateBomb() signature since they are empowered to detonate bombs. One detonate bombs in the Whitehouse, the other in Bin Laden's palace, and the other, since he is a double agent, can detonate bombs in both places.
Do they share the same 'theme' of world peace or goody-goody-two-shoes? No.
Do they share the same ability to detonate bombs? Yes.
So should we use interface or abstract class? Interface!
![Thumbs Up](images/smilies/thumbsup.gif)
yey!