4-3 more OOP not done in class
Download
Report
Transcript 4-3 more OOP not done in class
OOP, Round 3
Step in to the ring with OOP
CS 102-02
Lecture 5-3
May 1, 1998
CS102-02
Lecture 5-3
Agenda
• Two kinds of inheritance
• Interfaces in Java
• The End of OO
May 1, 1998
CS102-02
Lecture 5-3
Polymorphism & Abstract
Classes
• Employee class is abstract
public abstract class Employee
– Can't create Employee objects
– Can create references to Employee objects
• Create new kinds of employees
– Managers, piece workers, commission
workers, ...
May 1, 1998
CS102-02
Lecture 5-3
The New Employee on the Block
• Create a new Employee subclass
tomorrow
• Use an Employee reference to point to it
• Java takes care of the many employees
with polymorphism
May 1, 1998
CS102-02
Lecture 5-3
Two Kinds of Inheritance
• Implementation inheritance
– "Here's what you can do, and here's how
you do it"
– In Java, use extends
• Interface inheritance
– "Here's what you can do, figure out how to
do it yourself"
– In Java, use implements
May 1, 1998
CS102-02
Lecture 5-3
Implementation Inheritance
• Using extends means you inherit both:
– Existence of a method
– The method's implementation
• Applet example
– The Applet class includes many methods
(e.g. play())
– Don't have to write play() in your subclass
May 1, 1998
CS102-02
Lecture 5-3
Interface Inheritance
• Interface is a contract that says what
you have to do, but not how to do it
• An event example
Interface java.awt.event.ActionListener
• Interface includes one method:
actionPerformed(ActionEvent)
– Invoked when an action occurs
• If you wanna be an ActionListener, ya
gotta have actionPerformed()
May 1, 1998
CS102-02
Lecture 5-3
Interfacing with Interfaces
• Interface inheritance is still is-a
public class Comparison extends
Applet implements ActionListener
– Comparison is-a Applet AND
Comparison is-a ActionListener
• Take a look at the init() method of
Comparison...
May 1, 1998
CS102-02
Lecture 5-3
The Trusty Comparison Applet
public class Comparison extends Applet
implements ActionListener {
public void init() {
:
prompt2=new Label( "Enter an integer" );
add( prompt2 ); //Put prompt2 on applet
input2 = new TextField( 10 );
input2.addActionListener( this );
add( input2 );
// put input2 on applet
}
:
}
May 1, 1998
CS102-02
Lecture 5-3
Adding a Listener
• input2 is a Label so check the Label
class
public synchronized void
addActionListener(ActionListener l)
– Adds the specified action listener to
receive action events from this text field.
• Where does the ActionListener
come from?
May 1, 1998
CS102-02
Lecture 5-3
Single & Multiple Inheritance
• Java only supports single
implementation inheritance
– Only one class can be subclassed with
extends
• Multiple interface inheritance
– Any number of interfaces can be
implemented
– JumpingBox implements MouseListener
and MouseMotionListener
May 1, 1998
CS102-02
Lecture 5-3
Interface vs. Implementation
• Implementation works well for core
functionality
– Degree of specification
• Interface is good for picking and
choosing
– Choose just the interfaces you want
– Why bother with interfaces -- why not just
implement methods willy-nilly?
May 1, 1998
CS102-02
Lecture 5-3
Uses for Interfaces
• Defining related methods
• Grouping constants together
interface
public
public
public
}
PowersOfTwo {
static final int TWO_EIGHT = 256;
static final int TWO_TEN = 1024;
static final int TWO_SIXTEEN = 65536;
• Markers
– Some interfaces don't do anything, just
mark a class
May 1, 1998
CS102-02
Lecture 5-3
A Few of My Favorite Things
• Brown paper packages, tied up in string
• Wrapper classes
– Primitive types
– Legacy code
May 1, 1998
CS102-02
Lecture 5-3
Object Magic
• In Java, almost everything is an object
– Primitive types aren't classes
– Wrapper classes are "objectified" versions
of non-object entities
• Primitive type int has a wrapper:
Integer
– Integers are like int objects
– Extra features like parseInt()
May 1, 1998
CS102-02
Lecture 5-3
Composition in Action
• From the definition of the Integer class:
// The value of the Integer.
private int value;
public Integer(int value) {
this.value = value;
}
May 1, 1998
CS102-02
Lecture 5-3
Other Wrappers in Java
• Mostly numbers, because those are the
main primary types
– BigDecimal, BigInteger, Byte, Double,
Float, Integer, Long, Short
May 1, 1998
CS102-02
Lecture 5-3
The Legacy of Big Iron
• Web (and Java) puts a new face on old
systems
• How do you make customer
transactions processed with a CICS
system on an IBM 390 running MVS
accessible over a Web server?
Wrap it!
May 1, 1998
CS102-02
Lecture 5-3
Web Wrappers
• Create a set of Java classes with same
behaviors and data as existing system
• Write programs to communicate
between Java objects and legacy
systems
• Build new systems with Java objects
instead of legacy systems
May 1, 1998
CS102-02
Lecture 5-3
The End of OO
• Next week:
– String class: Lots of features for handling
strings, good example of using classes
– Start on graphics
May 1, 1998
CS102-02
Lecture 5-3