C SC 225 Lecture 4: Topics in Polymorphism
[ previous
| schedule
| next ]
Review from CSC 160 and 205
- Most of the concepts in Horstmann chapter 4 are review for us
- Many of the applications are not
Java interface
- What a Java interface is
- What an interface can and cannot contain
- Why use an interface?
- Use of interface in declaring reference variable
- What polymorphic power comes from this?
- What limits are placed on its use?
- Use of interface in declaring (formal) parameter
- What polymorphic power comes from this?
- What limits are placed on its use?
- Interfaces and the implements clause
- Number of interfaces that a class can implement
- Sub-interface uses extends, not implements
- Number of interfaces that a sub-interface can extend
Comparable and Comparator
- Both used to define a "natural ordering", for sorting
- Collections.sort() static method
- Key in SortedMap, in Java Collections Framework
- Both are parameterized (as of Java 1.5)
- What is the difference between Comparable and Comparator ?
Anonymous Objects and Classes
- Example of anonymous object? Constructor call as argument to any method accepting an object argument.
- Example of anonymous class? Definition of action listener as argument to addActionListener()
- Both are in-lined, single-use definition
- Advantages?
- Drawbacks?
- Note that anonymous class is always an inner class
- These three techniques are all equivalent
-
// Named inner class, anonymous object
class Help {
void gui() {
JButton help = new JButton("help");
class HelpListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// do something here
}
}
help.addActionListener(new HelpListener());
}
}
-
// Anonymous inner class, named object
class Help {
void gui() {
JButton help = new JButton("help");
ActionListener helpAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do something here
}
} ;
help.addActionListener(helpAction);
}
}
-
// Anonymous inner class, anonymous object
class Help {
void gui() {
JButton help = new JButton("help");
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do something here
}
} );
}
}
- In all three examples:
- The inner class has access to all fields of the enclosing Help class
- The inner class has access to only final parameters and local variables of the enclosing gui() method
(remarkable in that the inner class may reference them long after the method has returned and they are "gone"!)
Polymorphism and Graphics
- Paint methods are used to draw all graphical entities
- Paint methods include a Graphics parameter (java.awt.Graphics)
- The Graphics parameter is called the graphics context
- The graphics context is what really does the drawing and paint methods use it
- Don't worry about where the graphics context was created; just use it and pass it on when necessary
- java.awt.Graphics2D class
- It turns out the argument, the actual graphics context object, is a Graphics2D object
- Graphics2D is a child class of Graphics, and defines additional drawing methods
- To use the Graphics2D methods, you have to typecast the graphics context parameter
- The Graphics2D class has draw() and fill() methods that
work for any Shape (an interface)
- The Graphics2D methods are polymorphic through their parameter
- The Graphics class defined a different method for each specific shape (e.g. line, rectangle, etc)
- By using Graphics2D, your paint method can be more general: it doesn't need to know at
compile time what kinds of shapes it is drawing!
Horstmann comprehensive example: the vehicle animation
- Pulls together the following elements:
- Animation occurs by periodically repositioning and repainting an icon
- The icon is field of a JLabel object
- ShapeIcon class implements the javax.swing.Icon interface
- ShapeIcon methods draw a MoveableShape object
- MoveableShape is an interface
- CarShape class implements the MoveableShape interface
- Thus the icon that is repositioned and repainted is a CarShape object
- Define anonymous inner classes for action listener and timer
- Action listener attached to Timer
- Timer is a thread (we'll study these in detail later in the course)
- At each timer tick (100 ms), the icon is moved and repainted.
- The entire animation mechanism is tied to the CarShape object only through the MoveableShape interface!
- Thus any moveable shape can be animated using this mechanism!
- How to change the thing being animated from a car to a bicycle?
- The animation mechanism is "de-coupled" from the object being animated! Very powerful, thanks for polymorphism!
- The applicable proverb is "Program to the interface!" (We will see this repeatedly in HFDP)
[ C
SC 225 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)