15 - Click to edit Master subtitle style Chapter Fifteen...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Fifteen Modern Programming Languages, 2nd ed. A Second Look At Java Chapter Fifteen Modern Programming Languages, 2nd ed. 11 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 Chapter Fifteen Modern Programming Languages, 2nd ed. 22 Person x; 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 Chapter Fifteen Modern Programming Languages, 2nd ed. 33 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 Chapter Fifteen Modern Programming Languages, 2nd ed. 44 public interface Drawable { void show(int xPos, int yPos); void hide(); } 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 Chapter Fifteen Modern Programming Languages, 2nd ed. 55 Chapter Fifteen Modern Programming Languages, 2nd ed. Examples Chapter Fifteen Modern Programming Languages, 2nd ed. 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 … } 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: Chapter Fifteen Modern Programming Languages, 2nd ed. 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); 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 Chapter Fifteen Modern Programming Languages, 2nd ed. 88 static void flashoff(Drawable d, int k) { for (int i = 0; i < k; i++) { d.show(0,0); d.hide(); } } Chapter Fifteen Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 58

15 - Click to edit Master subtitle style Chapter Fifteen...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online