Re: Interfaces Question - I am missing something
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 06 May 2008 15:58:48 -0700
On Tue, 06 May 2008 14:25:21 -0700, jmDesktop <needin4mation@xxxxxxxxx> wrote:
[...]
Now, what I don't get is why this is so great. I understand it looks
better than the else if construct and the JVM calls the individual
classes that implement the behavior that are of the Washable "type"
interface, with there methods in a cleaner way, but...You still have
to create a wash() method for each class that wo.wash() calls, so how
did I make out better?
Well, one of the most obvious advantages you seem to be missing is that in the non-polymorphic example you provided, you would need to update the method any time you added a new class that was "washable". With an interface, _any_ class can implement the interface, and code that relies on the interface will "just work". It will work with any implementation of the interface, including implementations made long after the code using the interface was written.
As a classic example, see all the "listener" type interfaces in Java. These are very common in AWT and Swing, as a way of allowing arbitrary classes to hook into events that might occur within the GUI framework. Using interfaces, developers like you and me can write classes that respond to these events without having to call up Sun and say "hey, I've got this class I want added to your run-time implementation that's going to listen to events in your JFrame class".
From a more general point of view, this idea is a central focus of OOP programming overall. It's not just interfaces. Abstract and virtual methods create similar situations where the caller of the method doesn't need to know anything about what class actually implemented the method. All they need to know is the base class that the implementation inherited. Again, the actual implementation can be done long after the code that uses the implementation was written, because it complies with the contract defined in the base class that the using code _does_ know about.
In Java, all methods are virtual by default, making this sort of thing that much more common and important (admittedly, that's not always a good thing :) ).
Interfaces provide the same sort of functionality, but without the restriction that you can only inherit/implement one of them (which is a restriction of classes). In some sense they are also more "pure", since an interface _never_ defines any part of the implementation. They are _only_ a contract.
For what it's worth, since you're asking the question I assume that you're relatively new to OOP. Once you've done more OOP and taken advantage of abstract and virtual base methods/classes, and perhaps even worked with interfaces themselves, I think it will become more clear why they are useful. It's sometimes hard to explain the practical advantages to someone who's never had a chance to really work with code that takes advantage of them (or with code that doesn't :) ).
Pete
.
- References:
- Interfaces Question - I am missing something
- From: jmDesktop
- Interfaces Question - I am missing something
- Prev by Date: Re: Interfaces Question - I am missing something
- Next by Date: Naming convention for collection of objects
- Previous by thread: Re: Interfaces Question - I am missing something
- Next by thread: Re: Interfaces Question - I am missing something
- Index(es):
Relevant Pages
|