Announcements - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Announcements

Description:

Classification lets humans deal with complexity, inheritance is a ... General to specific, e.g., cat to siamese. Lecture 27: Abstract Classes & Inheritance ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 10
Provided by: CSCF
Learn more at: http://www.cs.utexas.edu
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
  • Objects - attributes behaviors
  • Classification - Inheritance
  • Today
  • Review Inheritance
  • Abstract Classes

2
Why Inheritance?
  • Reuse of common functionality in related classes
  • Reducing redundancy eases programmer effort
  • Classification lets humans deal with complexity,
    inheritance is a form of classification
  • General to specific, e.g., cat to siamese

3
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
4
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(...)

5
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

6
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

7
Abstract Classes
  • How can we partially declare a class?
  • Example
  • Shape class with subtypes Rectangle and Triangle
  • Is there a default draw method?
  • Is there a default toString?

8
Syntax for Abstract Classes
  • public abstract class Shapes
  • protected static String name // Rectangle,
    Triangle, ...
  • public abstract String toString() // must
    implement toString
  • public String getName()
  • return name
  • public class Rectangle extends Shapes
  • protected static String name Rectangle
  • ....
  • public String toString()
  • return name

9
BlueJ examples ...
Write a Comment
User Comments (0)
About PowerShow.com