aspose file tools*
The moose likes Beginning Java and the fly likes Questions about polymorphism and interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Questions about polymorphism and interfaces" Watch "Questions about polymorphism and interfaces" New topic
Author

Questions about polymorphism and interfaces

John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
From my book ( the name escapes me at the moment), one example of polymorphism is using an ArrayList of different Class Objects by referring to the Superclass of those Objects, making the code easier to read.

Is there another use for polymorphism?



And about Interfaces. All I can understand is that an Interface is full of abstract methods and any class that implements this interface must implement all of its methods. I'm trying to tie this with listeners and how classes implement them. If an interface is full of abstract methods, how does "implementing" a listener give the listening functionality?



Thanks in advance!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

The point of polymorphism is that you can provide a method with a specific implementation of the type, without the method having to care about what the actual type is.

Listeners are a great example. If you want to react to events from the user interface, the user interface needs something that will handle the events. It really doesn't care what it is, as long as it accepts events and deals with it.

JButton's addActionListener for instance accepts an instance of ActionListener. When you click the button, it will pass an ActionEvent instance to your ActionListener's actionPerformed() method. You can then react to the event in a specific way, by providing a specific implementation of actionPerformed in your custom ActionListener subclass.
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
But I still don't understand.

It looks to me that interfaces are just a bunch of abstract methods.

How does the ActionListener interface have that kind of functionality?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

John Quach wrote:But I still don't understand.
It looks to me that interfaces are just a bunch of abstract methods.
How does the ActionListener interface have that kind of functionality?

It doesn't, but whatever class implements it must do.

The classic example of polymorphism goes as follows:
I have a bunch of shapes that I want to draw, and I simply want to process them all from a list.
The first thing is to require that all shapes have a method to draw, so:Now I have to set up some actual Shapes:I now have a bunch of classes whose supertype is Shape, so I can use polymorphism to draw them out, viz:and, assuming the draw() methods are OK, all my shapes will be drawn correctly. You can actually test it very easily by just having each method print out a line with the name of the Shape being "drawn".

Do you see the power? Rather than my code having to check what type each one is at runtime and call the appropriate method, it's implicit in the design. I can add 17 more types of Shape and a thousand Shape objects to my List, and the same code will still work.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
Yeah I know.

But how does implementing ActionListener give you the ability to add listeners to an object? Or how does implementing Runnable gives you methods for Threads?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

John Quach wrote:Or how does implementing Runnable gives you methods for Threads?

Let's deal with that one, because it's easier. Listener is part of a pattern that involves callbacks, so it takes a bit more thought.

Q: How do you start a Runnable?

A: You start it by creating a Thread that takes your Runnable as a parameter.

Thread is not an interface, it's a fully-functioning class; so it's quite reasonable that it might "understand" how to start a Runnable, since a Runnable class must implement a run() method (because it's declared in the interface).

So essentially what you're saying to the new Thread is: "here's a Runnable object; run its run() method for me".
The polymorphic part is that the Thread doesn't need to know what type of object it's running; all it needs to know is that it has a run() method. I can have 563 different types of Runnable, and the way I run them will be identical.

And furthermore, the mechanism is identical to that with the Shape example: In that case, I (you) created the interface; in this case the Java designers have provided it for you; and what they both are is abstractions that allow a client (in the case of the Shape, a for loop; in the case of Runnable, a Thread class) to know only what it needs to fulfill a task (in the case of Shape: how to draw() it; in the case of Runnable: how to run() it).

If you follow that, then we can go on to Listeners.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions about polymorphism and interfaces