What's the value of an interface if you have to define the methods?

What's the value of an interface if you have to implement all of the methods in the extended class? I mean why not just add those methods individually to the classes that you are developing. I just don't see the value in extending something I have to rewrite anyhow.
[282 byte] By [joseph_mueller] at [2007-9-26 2:23:32]
# 1

The value is that you can any class that implements the interface can be cast to that interface.

For example if you are using graphics you will want to us the Shape interface. The shape interface cannot be instantiated of course. You can then create your own class and implement the shape interface. Then you can pass your class to methods that accept Shape arguments and the compliler knows that the functions in Shape are defined in your interface. It allows for great flexiblity and extensiblilty in your code.

Another example is the MouseListener interface. If you have a window and you want to capture mouse events on it, you can say that your window class implements MouseListener. Then you just have to add a few functions to your window and the compiler is happy because it knows the methods it needs are there.

dubwai at 2007-6-29 9:31:42 > top of Java-index,Archived Forums,Java Programming...
# 2

> What's the value of an interface if you have to

> implement all of the methods in the extended class? I

> mean why not just add those methods individually to

> the classes that you are developing. I just don't see

> the value in extending something I have to rewrite

> anyhow.

Hi.

Here's a brief and very general example of what interfaces are good for:

Sometimes, the code you develop doesn't know - or does not have to know - the exact type [class] of the runtime objects it manipulates. This kind of situation gets handled easier by the use of interfaces : the objects that your code manipulates are implementing a common interface - or set of interfaces - regardless of their concrete classes.

For further details please see any book/article related to OO Design Patterns.

Regards,

Rick

amihailescu at 2007-6-29 9:31:42 > top of Java-index,Archived Forums,Java Programming...
# 3

> What's the value of an interface if you have to

> implement all of the methods in the extended class? I

> mean why not just add those methods individually to

> the classes that you are developing. I just don't see

> the value in extending something I have to rewrite

> anyhow.

Hi.

Here's a brief and very general example of what interfaces are good for:

Sometimes, the code you develop doesn't know - or does not have to know - the exact type [class] of the runtime objects it manipulates. This kind of situation gets handled easier by the use of interfaces : the objects that your code manipulates are implementing a common interface - or set of interfaces - regardless of their concrete classes.

For further details please see any book/article related to OO Design Patterns.

Regards,

Rick

amihailescu at 2007-6-29 9:31:42 > top of Java-index,Archived Forums,Java Programming...
# 4

> What's the value of an interface if you have to

> implement all of the methods in the extended class? I

> mean why not just add those methods individually to

> the classes that you are developing. I just don't see

> the value in extending something I have to rewrite

> anyhow.

Hi.

Here's a brief and very general example of what interfaces are good for:

Sometimes, the code you develop doesn't know - or does not have to know - the exact type [class] of the runtime objects it manipulates. This kind of situation gets handled easier by the use of interfaces : the objects that your code manipulates are implementing a common interface - or set of interfaces - regardless of their concrete classes.

For further details please see any book/article related to OO Design Patterns.

Regards,

Rick

amihailescu at 2007-6-29 9:31:42 > top of Java-index,Archived Forums,Java Programming...
# 5
Sorry for the multiple posting ...Rick
amihailescu at 2007-6-29 9:31:42 > top of Java-index,Archived Forums,Java Programming...