• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions about polymorphism and interfaces

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Saloon Keeper
Posts: 15487
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
John Quach
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic