Docsity

Java Programming: Understanding Inheritance, Interfaces, and Generics through Examples, Slides of Advanced Computer Programming

Partial preview of the text

Download Java Programming: Understanding Inheritance, Interfaces, and Generics through Examples and more Slides Advanced Computer Programming in PDF only on Docsity!

A Second Look At Java

Chapter Fifteen Modern Programming Languages, 2nd ed. (^) Docsity.com 1

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

Person x;

Docsity.com

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

public interface Drawable {

void show(int xPos, int yPos);

void hide();

Docsity.com

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. (^) Docsity.com 5

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

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);

Docsity.com

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

static void flashoff(Drawable d, int k) { for (int i = 0; i < k; i++) { d.show(0,0); d.hide(); } }

Docsity.com

Chapter Fifteen Modern Programming Languages, 2nd ed. 10

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

Docsity.com

Chapter Fifteen Modern Programming Languages, 2nd ed. 11

  • Remove one String from the worklist and return
  • it. There must be at least one element in the
  • worklist.
  • @return the String item removed */ String remove(); }

Docsity.com

Chapter Fifteen Modern Programming Languages, 2nd ed. 13

  • A Node is an object that holds a String and a link
  • to the next Node. It can be used to build linked
  • lists of Strings. */ public class Node { private String data; // Each node has a String... private Node link; // and a link to the next Node

/**

  • Node constructor.
  • @param theData the String to store in this Node
  • @param theLink a link to the next Node */ public Node(String theData, Node theLink) { data = theData; link = theLink; }

Docsity.com

Chapter Fifteen Modern Programming Languages, 2nd ed. 14

  • Accessor for the String data stored in this Node.
  • @return our String item */ public String getData() { return data; }

/**

  • Accessor for the link to the next Node.
  • @return the next Node */ public Node getLink() { return link; } }

Docsity.com

Chapter Fifteen Modern Programming Languages, 2nd ed. 16

  • Test whether this stack has more elements.
  • @return true if this stack is not empty */ public boolean hasMore() { return (top!=null); }

/**

  • Pop the top String from this stack and return it.
  • This should be called only if the stack is
  • not empty.
  • @return the popped String */ public String remove() { Node n = top; top = n.getLink(); return n.getData(); } } Docsity.com

A Test

 Output: The cut worm forgives the plow.

 Other implementations of Worklist are

possible: Queue, PriorityQueue, etc.

Chapter Fifteen Modern Programming Languages, 2nd ed. 17

Worklist w; w = new Stack(); w.add("the plow."); w.add("forgives "); w.add("The cut worm "); System.out.print(w.remove()); System.out.print(w.remove()); System.out.println(w.remove());

Docsity.com

More Polymorphism

 Another, more complex source of

polymorphism

 One class can be derived from another,

using the keyword extends

 For example: a class PeekableStack

that is just like Stack, but also has a

method peek to examine the top element

without removing it

Chapter Fifteen Modern Programming Languages, 2nd ed. (^) Docsity.com 19

Chapter Fifteen Modern Programming Languages, 2nd ed. 20

  • A PeekableStack is an object that does everything a
  • Stack can do, and can also peek at the top element
  • of the stack without popping it off. */ public class PeekableStack extends Stack {

/**

  • Examine the top element on the stack, without
  • popping it off. This should be called only if
  • the stack is not empty.
  • @return the top String from the stack */ public String peek() { String s = remove(); add(s); return s; } }

Docsity.com