200 likes | 227 Views
OOP, Round 3. Step in to the ring with OOP. CS 102-02 Lecture 5-3. Agenda. Two kinds of inheritance Interfaces in Java The End of OO. Polymorphism & Abstract Classes. Employee class is abstract public abstract class Employee Can't create Employee objects
E N D
OOP, Round 3 Step in to the ring with OOP CS 102-02 Lecture 5-3
Agenda • Two kinds of inheritance • Interfaces in Java • The End of OO
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, ...
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
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
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
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()
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...
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 } : }
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?
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
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?
Uses for Interfaces • Defining related methods • Grouping constants together interface PowersOfTwo { public static final int TWO_EIGHT = 256; public static final int TWO_TEN = 1024; public static final int TWO_SIXTEEN = 65536; } • Markers • Some interfaces don't do anything, just mark a class
A Few of My Favorite Things • Brown paper packages, tied up in string • Wrapper classes • Primitive types • Legacy code
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()
From the definition of the Integer class: Composition in Action // The value of the Integer. private int value; public Integer(int value) { this.value = value; }
Other Wrappers in Java • Mostly numbers, because those are the main primary types • BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
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!
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
The End of OO • Next week: • String class: Lots of features for handling strings, good example of using classes • Start on graphics