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

A chapter from 'modern programming languages, 2nd edition' that explains the concepts of extending classes, implementing interfaces, multiple inheritance, and generics in java through examples and code snippets. It covers the use of interfaces as reference types, implementing a worklist interface, and creating a stack class that implements the worklist interface.

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani 🇮🇳

4.5

(2)

83 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Second Look At Java
Chapter Fifteen Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

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