J
jmDesktop
I'm trying to understand the benefit of interfaces (the oop kind). I
understand that:
they allow different types of objects to be grouped by behavior
instead of relying strictly on inheritance (a kind of multi-
inheritance)
They allow for more polymorphism, as opposed to just inheritance.
What I don't understand (please correct the above if I am wrong
thanks), is why I need them. I'm just missing something obvious.
I was almost getting it here:
http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html
In the middle of the page it has Interfaces. It talks about things
being "Washable." It shows some hideous code of what you'd have to do
if you didn't use Interfaces (lots of if else ifs):
// In Source Packet in file interface/ex2/Cleaner.java
class Cleaner {
// (This doesn't use polymorphism)
public static void cleanAnObject(Object obj) {
// Perform any necessary processing of the
// object before washing...
// Wash the object
if (obj instanceof Cup) {
// (Here you are using polymorphism, but just
// within the Cup family.)
((Cup) obj).wash();
}
else if (obj instanceof Dog) {
((Dog) obj).wash();
}
else if (obj instanceof Window) {
((Window) obj).wash();
}
else if (obj instanceof Car) {
((Car) obj).wash();
}
// Else the object doesn't get washed
// Perform other processing on the object to
// complete the cleaning process...
}
}
In the end is the payoff with:
// In Source Packet in file interface/ex3/Cleaner.java
class Cleaner {
public static void cleanAnObject(WashableObject wo) {
//...
wo.wash();
//...
}
}
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? I just got rid of the ugly code for the OOP
design, but why? I think it has to do with extensibility because if
someone added to the program they would need another if else added to
the ugly code. I don't believe it's just because it's OOP for the
sake of OOP. I'm missing it.
I keep reading it is a contract, but how? What couldn't a designer
just create a wash2() method and do something there. How did it help?
Thank you for helping me.
understand that:
they allow different types of objects to be grouped by behavior
instead of relying strictly on inheritance (a kind of multi-
inheritance)
They allow for more polymorphism, as opposed to just inheritance.
What I don't understand (please correct the above if I am wrong
thanks), is why I need them. I'm just missing something obvious.
I was almost getting it here:
http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html
In the middle of the page it has Interfaces. It talks about things
being "Washable." It shows some hideous code of what you'd have to do
if you didn't use Interfaces (lots of if else ifs):
// In Source Packet in file interface/ex2/Cleaner.java
class Cleaner {
// (This doesn't use polymorphism)
public static void cleanAnObject(Object obj) {
// Perform any necessary processing of the
// object before washing...
// Wash the object
if (obj instanceof Cup) {
// (Here you are using polymorphism, but just
// within the Cup family.)
((Cup) obj).wash();
}
else if (obj instanceof Dog) {
((Dog) obj).wash();
}
else if (obj instanceof Window) {
((Window) obj).wash();
}
else if (obj instanceof Car) {
((Car) obj).wash();
}
// Else the object doesn't get washed
// Perform other processing on the object to
// complete the cleaning process...
}
}
In the end is the payoff with:
// In Source Packet in file interface/ex3/Cleaner.java
class Cleaner {
public static void cleanAnObject(WashableObject wo) {
//...
wo.wash();
//...
}
}
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? I just got rid of the ugly code for the OOP
design, but why? I think it has to do with extensibility because if
someone added to the program they would need another if else added to
the ugly code. I don't believe it's just because it's OOP for the
sake of OOP. I'm missing it.
I keep reading it is a contract, but how? What couldn't a designer
just create a wash2() method and do something there. How did it help?
Thank you for helping me.