|Like A Class||Gotchas|
|extend or implement?||Interface Or Abstract?|
|Sample Code||Easy Maintenance|
|Multiple Inheritance||Reusable Code|
|Implied Modifiers||Compilation Efficiency|
|Business Metaphor||What You Don’t Specify|
|Hymn of Praise|
I will develop this metaphor that will only gradually come clear over the years as you experiment with interfaces. An interface is like a role description. A person in a bureaucracy may play several roles: filing clerk, keeper of petty cash, manager of the coffee machine. You are restricted to a subset of her total capabilities when you interact with her in each of her roles.
For someone to temporarily take over as manager of the coffee machine, all they need assume is the one role, not all the capabilities of the usual multi-talented coffee lady. The role description describes the minimal capabilities for someone to pinch hit as a coffee machine manager. Even a male can be the coffee lady.
With clearly defined roles, it is easy to ask, do you have the capabilities to pinch hit as coffee lady? You don’t have to check out the necessary skills one by one. An interface is like a skill set summary.
In a hierarchical business organisation, each person has a formal set of responsibilities, information to be passed to superiors, information to be gleaned from underlings, commands taken from superiors, and commands given to underlings… Most people in an organisation play many roles, depending on whom they are talking to. Underlings are severely restricted in the services they can demand of the president. Interfaces formally define the precise set of services each class must provide in a given role. Roles simplify interactions. The requestor knows what services are guaranteed to be provided, and interfaces prevent the requestor from asking for services outside the formally agreed on role, even if the requestee is capable of providing them.
With interfaces, it makes who fulfills a role interchangeable. Nobody informally comes to rely on the additional petty cash skills of the original coffee lady, unless they formally interacted with the coffee lady also in the role of petty cash manager. Different people could pinch hit for a vacationing coffee lady, one taking over her coffee lady role and one taking over her petty cash manager role. Interfaces allow a class to say to other classes, if you want to interact with me in role A, you must be capable of assuming roles B, C and D. It makes it much simpler to plug classes together if all you have to do is find a matching set of compatible roles (interfaces). If they won’t fit, it is clear what minimal work you would have to do to upgrade the skill set (methods), to allow a class to act in a new role, and hence to allow it to interact with some other particular class.
Role descriptions, like interfaces, can be nested (multiply inherited). The role description for a telephone supervisor naturally assumes capability of also acting as an operator, and also the role of petty cash manager.
You could use Java’s duct tape — the interface. You would define an interface say GeneralApplet with some of the methods common to Applet and JApplet. You would then modify both Applet and JApplet to implement your GeneralApplet interface. This is very easy to do, just add the words implements GeneralApplet to the class definition line of both Applet and JApplet. You now have a GeneralApplet reference that could be either an Applet or JApplet decidedYou would have glued together classes that came from quite different ancestries. If necessary, you can mask slight differences in the method signatures of the two classes with some interface wrapper methods.
You will find yourself using interfaces more and more as you work on complex projects. They let you simplify the possible interactions between groups of classes to just what is needed. Interfaces formalise, document and enforce the interactions between classes. They make code components more independent and reusable. Pieces of code can be written even when the code it needs to interact with is not written yet or is currently not compilable. In GUI (Graphic User Interface) s, where you have panels within panels communicating, you will find it helps immensely to define an interface for each parent listener callback delegate, even when there is only one caller class.
Interfaces give a much finer degree of control of how classes interact than the public/ private scheme.
When you use an interface, you lower the bar to potential class interactions, and hence reusability. You demand fit only where fit really matters.
You don’t need to specify public in the interface method definitions. That is presumed. However, when you go to implement the interface, you need to specifically spell out the public, so I find it easier to specify public in the interface to make them more useful templates. IDEs (Integrated Development Environments) today are getting smarter and will insert it for you when you use wizards to generate a skeleton implementation of an interface.
I don’t recommend this, but you can also leave off the static finals on your interface constants.
You don’t specify synchronized in your interface method declarations. This is an implementation detail.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[126.96.36.199]
|Feedback||You are visitor number 192,682.|