Announcements - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Announcements

Description:

http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html. Last Time ... { protected int width; // the instance variables. protected int height; ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 11
Provided by: CSCF
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements Review
  • Lab 8 Due Thursday
  • Image and color effects with 2D arrays
  • Read
  • Chapter 9 Cahoon Davidson
  • Chapter 9 Regis Stepp
  • Additional Reading
  • http//java.sun.com/docs/books/tutorial/java/conce
    pts/inheritance.html
  • Last Time
  • Review Inheritance
  • Dynamic dispatch
  • Today
  • Dynamic dispatch
  • Abstract Classes

2
Inheritance in Java ...
  • All classes inherit from Object
  • Rectangle inherits from Object
  • ColoredRectangle inherits from
  • Rectangle

Object provides these methods toString()
equals() // more on these clone()
// later... instanceof()
Object
Transformation
Graph
Rectangle
ColoredRectangle
3
Super classRectangle
  • public class Rectangle
  • protected int width // the instance
    variables
  • protected int height
  • public Rectangle() width 0 height 0
  • public Rectangle(int w, int h) width w
    height h
  • public void draw(...)

4
Syntax of InheritanceExample ColoredRectangle
  • public class ColoredRectangle extends Rectangle
  • private Color myColor // additional
    instance variables
  • public ColoredRectangle()
  • super() // optional
  • myColor Color.white
  • / Is the same as
  • public ColoredRectangle() implicitly first
    calls super, because

  • the signatures match
  • myColor Color.white
  • /
  • public ColoredRectangle(int w, int h, Color
    newColor)
  • super(w,h) // required why do we need
    this one?
  • myColor newColor

5
Dynamic Dispatch
  • Declare variables of supertype and they can be
    the supertype or the subtype
  • Rectangle r new ColoredRectangle()
  • Declare variables of a subtype and they can only
    be of type subtype or lower.
  • Test type with instanceof
  • ltvariable instanceof TypeNamegt returns a boolean
  • if (r instanceof Rectangle) ...
  • if (r instanceof ColoredRectangle) ...
  • BlueJ

6
Abstract Classes
  • Want to partially declare a class
  • Force an implementation of a method but do not
    offer a default implementation it
  • require a draw method, but have not default draw

7
Syntax for Abstract Classes
  • public abstract class Shapes
  • protected static String name // e.g.,
    Rectangle, Triangle, ...

  • // Subclass must implement draw
  • public abstract void draw(DrawingBox box)
  • public String getName() // Subclass
    need not implement getName
  • return name
  • public class Rectangle extends Shapes
  • protected static String name Rectangle
  • ....
  • public void draw(DrawingBox box)
  • box.drawRect(...)

8
BlueJ examples ...
9
Interfaces
  • Want to partially specify a behavior that can
    apply to lots of classes
  • For example, adding Color
  • Enforces a consistant application of a feature
    across lots of classes

10
Interfaces
  • Interface Colorable
  • public void setColor(Color c)
  • public Color getColor()
  • public class ColorRectangle extends Shapes
    implements Colorable
  • Color myColor
  • public void setColor(Color c)
  • myColor c
  • public Color getColor()
  • return myColor
Write a Comment
User Comments (0)
About PowerShow.com