1 / 20

OOP, Round 3

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

sehlers
Télécharger la présentation

OOP, Round 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOP, Round 3 Step in to the ring with OOP CS 102-02 Lecture 5-3

  2. Agenda • Two kinds of inheritance • Interfaces in Java • The End of OO

  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, ...

  4. 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

  5. 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

  6. 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

  7. 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()

  8. 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...

  9. 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 } : }

  10. 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?

  11. 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

  12. 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?

  13. 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

  14. A Few of My Favorite Things • Brown paper packages, tied up in string • Wrapper classes • Primitive types • Legacy code

  15. 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()

  16. 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; }

  17. Other Wrappers in Java • Mostly numbers, because those are the main primary types • BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short

  18. 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!

  19. 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

  20. The End of OO • Next week: • String class: Lots of features for handling strings, good example of using classes • Start on graphics

More Related