15 - A Second Look At Java Click to edit Master subtitle...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter Fifteen Modern Programming Languages, 2nd ed. A Second Look At Java 11
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Fifteen Modern Programming Languages, 2nd ed. Subtype Polymorphism Does this declare x to be a reference to an object of the Person class? Not exactly—the type Person may include references to objects of other classes Java has subtype polymorphism 22 Person x;
Image of page 2
Chapter Fifteen Modern Programming Languages, 2nd ed. Outline 15.2 Implementing interfaces 15.3 Extending classes 15.4 Extending and implementing 15.5 Multiple inheritance 15.6 Generics 33
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Fifteen Modern Programming Languages, 2nd ed. Interfaces A method prototype just gives the method name and type—no method body An interface in Java is a collection of method prototypes 44 public interface Drawable { void show(int xPos, int yPos); void hide(); }
Image of page 4
Chapter Fifteen Modern Programming Languages, 2nd ed. Implementing Interfaces A class can declare that it implements a particular interface Then it must provide public method definitions that match those in the interface 55
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Fifteen Modern Programming Languages, 2nd ed. Examples 66 public class Icon implements Drawable { public void show(int x, int y) { … method body … } public void hide() { … method body … } …more methods and fields… } public class Square implements Drawable, Scalable { … all required methods of all interfaces implemented … }
Image of page 6
Chapter Fifteen Modern Programming Languages, 2nd ed. Why Use Interfaces? An interface can be implemented by many classes: Interface name can be used as a reference type: 77 public class Window implements Drawable … public class MousePointer implements Drawable … public class Oval implements Drawable … Drawable d; d = new Icon("i1.gif"); d.show(0,0); d = new Oval(20,30); d.show(0,0);
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Fifteen Modern Programming Languages, 2nd ed. Polymorphism With Interfaces Class of object referred to by d is not known at compile time It is some class that implements Drawable , so it has show and hide methods that can be called 88 static void flashoff(Drawable d, int k) { for (int i = 0; i < k; i++) { d.show(0,0); d.hide(); } }
Image of page 8
Chapter Fifteen Modern Programming Languages, 2nd ed. A More Complete Example A Worklist interface for a collection of String objects Can be added to, removed from, and tested for emptiness 99
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Fifteen Modern Programming Languages, 2nd ed. 1010 public interface Worklist { /** * Add one String to the worklist. * @param item the String to add */ void add(String item); /** * Test whether there are more elements in the * worklist: that is, test whether more elements * have been added than have been removed. * @return true iff there are more elements */ boolean hasMore();
Image of page 10
Chapter Fifteen Modern Programming Languages, 2nd ed. 1111 /** * Remove one String from the worklist and return * it. There must be at least one element in the * worklist.
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern