Chapter 8: Understanding Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 8: Understanding Inheritance

Description:

So, we extended the solution to allow for the targets in the playing field to be moved. ... interface by creating a temporary ball at the point of the mouse press: ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 26
Provided by: BenSc
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8: Understanding Inheritance


1
Session 18
  • Chapter 8 Understanding Inheritance

2
Recall Exercise 2 From Tuesday
  • Its very annoying to move a target from the
    pallet and drop it in the wrong place.
  • So, we extended the solution to allow for the
    targets in the playing field to be moved.

3
Exercise 2 Solution
  • Recall that we needed to know if a mousePress
    occurred on a target. We tried to leverage the
    intersects method of PinBallTarget interface by
    creating a temporary ball at the point of the
    mouse press
  • Ball tempBall new Ball( e.getX(), e.getY(),
    1 )
  • for ( int j 0 j lt targets.size() j )
  • PinBallTarget target(PinBallTarget)
    targets.elementAt(j)
  • if ( target.intersects( tempBall ) )
  • startPressed 1
  • element target
  • // end if
  • // end for

4
Opening Exercise 2 Solution
  • We didnt want to create a PinBall because
    PinBalls get constructed with motion and a large
    size.
  • This forced us to change the PinBallTarget
    interface to accept the more general Ball
  • public interface PinBallTarget
  • public boolean intersects( Ball aBall )
  • public void moveTo ( int x, int y )
  • public void paint ( Graphics g )
  • public void hitBy ( PinBall aBall )
  • // end PinBallTarget
  • We also have to change the intersects methods in
    each of the targets
  • It turns out that we could have used a PinBall
    since it exists for such a short time

5
Question
  • If you had to pick one word that identifies the
    key concepts of this course so far, what would it
    be? Why?

6
My Answer
  • If you had to pick one word that identifies the
    key concepts of this course so far, what would it
    be? Why?
  • I think I might pick Inheritance

7
Inheritance
  • What?
  • A mechanism for reusing code in an existing
    class.
  • A mechanism for organizing kinds of objects that
    have the same implementation.
  • How?
  • Create a class that extends another class.
  • Why?
  • Who wants to rewrite code??
  • Reuse provides
  • reliability through continual testing
  • shorter development time
  • ability to build frameworks (dont call us...)
  • You can quickly build an application for
    demonstration purposes.

8
Another Exercise
  • On Page 132, Budd describes how we might
    implement the class Set as a subclass of class
    Vector. A Set lets you add an element to it only
    if the Set doesnt already contain that element.
  • What messages should a set respond to?

9
Another Exercise
  • On Page 132, Budd describes how we might
    implement the class Set as a subclass of class
    Vector. A Set lets you add an element to it only
    if the Set doesnt already contain that element.
  • addElement( Object )
  • removeElement( Object )
  • contains( Object )
  • isEmpty()
  • size()
  • If Set extends Vector, which of these are
    already in existence? Which are correct?
  • http//java.sun.com/j2se/1.4.2/docs/api/java/util/
    Vector.html

10
Another Exercise
  • On Page 132, Budd describes how we might
    implement the class Set as a subclass of class
    Vector. A Set lets you add an element to it only
    if the Set doesnt already contain that element.
  • addElement( Object )
  • removeElement( Object )
  • contains( Object )
  • isEmpty()
  • size()
  • Write the addElement method that we need to
    complete the Set class.

11
Another Exercise
  • On Page 132, Budd describes how we might
    implement the class Set as a subclass of class
    Vector. A Set lets you add an element to it only
    if the Set doesnt already contain that element.
  • addElement( Object )
  • removeElement( Object )
  • contains( Object )
  • isEmpty()
  • size()
  • If Set extends Vector What other problem(s) do
    we have?

12
A possible solution
  • import java.util.Vector
  • public class Set extends Vector
  • public Set()
  • super()
  • public void addElement( Object object )
  • if ( !contains( object ))
  • super.addElement( object )
  • public int indexOf( Object newElement )
  • System.out.println("Set.indexOf is not
    allowed." )
  • return -1
  • public Object elementAt( int index )
  • return null

13
Inheritance
  • In one sense, a subclass is an expansion of its
    superclass.
  • a subclass can add instance variables and
    methods
  • In another sense, a subclass is a contraction of
    its superclass.
  • a subclass defines a subset of instances of its
    superclass
  • In Java, all classes are subclasses, whether we
    say so or not. By default, any class that does
    not have an extends clause extends the class
    Object.
  • Consider our Ball hierarchy Ball, MovableBall,
    BoundedBall

14
  • public class Ball
  • private Rectangle location
  • private Color color
  • public Ball( int x, int y, int r ) ...
  • public void paint( Graphics g ) ...
  • public void setColor( Color newColor ) ...
  • protected Rectangle region() ...
  • protected Color color() ...
  • protected int radius() ...
  • protected int x() ...
  • protected int y() ...
  • protected void moveTo( int x, int y ) ...
  • public class MovableBall extends Ball
  • private double dx
  • private double dy
  • public MovableBall( int x, int y, int r ) ...
  • public void move() ...
  • public void setMotion( double ndx, double ndy )
    ...

15
Inheritance and Substitutability
  • An object X is substitutable for an object Y if
  • we can use X any place we use Y and
  • the client code not know the difference.
  • An example in practice, from the pinball games
  • The target vector holds any Object. Thats how
    Java Vectors work.
  • We put Springs, Walls, Holes, ..., into the
    vector.
  • When we retrieve objects from the vector, we
    treat them as PinBallTargets.

16
Inheritance and Substitutability
  • Another example in practice, from the cannon
    games
  • Our fire Button expects to be given an
    ActionListener that watches for button events.
  • We create FireButtonListener as an
    implementation of the ActionListener interface.
  • We add a FireButtonListener in place of an
    ActionListener.
  • What would the alternative be?

17
Substitutability
  • The common feature in these cases and the key
    to substitutability is that the objects share a
    common interface.
  • They respond to the same messages.
  • Inheritance and interfaces are mechanisms for
    ensuring the common interface.

18
Substitutability
  • So, why write our programs so that they use
    substitutable objects?
  • extendibility
  • flexibility
  • frameworks that implement a programs control
    while allowing programmers to add new objects to
    the program later
  • Of course, we can achieve these benefits without
    the use of inheritance and interfaces. But the
    compiler wouldnt be able to help us enforce them!

19
Types of Inheritance
  • Specialization
  • Essentially no new methods in the subclass.
  • Most subclass methods override inherited
    methods.
  • example our BoundedBall class
  • common in frameworks

20
Types of Inheritance
  • Specification
  • Superclass provides responsibility but no
    behavior.
  • Implement an interface or extend an abstract
    class.
  • example our event listeners
  • example pinball targets
  • private class MouseKeeper extends MouseAdapter
  • private PinBallTarget element
  • public void mousePressed ( MouseEvent e ) ...
  • public void mouseReleased( MouseEvent e ) ...

21
Number - Abstract Class Example
  • Parent class for numeric wrapper classes
    Integer, Long, Double, etc.
  • Subclasses must override abstract methods
  • public abstract class Number
  • public abstract int intValue()
  • public abstract long longValue()
  • public abstract float floatValue()
  • public abstract double doubleValue()
  • public byte byteValue() return (byte)
    intValue
  • public short shortValue()
  • return (short) intValue()
  • // end Number

22
Types of Inheritance
  • Extension
  • Subclass uses most or all inherited methods
    as-is.
  • Subclass adds new behavior and methods.
  • example our MovableBall class

23
Types of Inheritance
  • Combination
  • A class inherits from two or more classes. This
    is called multiple inheritance.
  • Some OOP languages provide it (C). Some
    dont (Java, Smalltalk).
  • Java does support combination through
    interfaces.
  • example Budds Hole class
  • class Hole extends Ball implements PinBallTarget
  • public Hole( int x, int y ) ...
  • public boolean intersects( Ball aBall ) ...
  • public void hitBy ( Ball aBall ) ...

24
Other Types of Inheritance
  • Limitation
  • The subclass primarily has methods that
    override inherited methods.
  • to restrict a behavior (example Square extends
    Rectangle)
  • to remove a behavior (example Set extends
    Vector)
  • Limitation violates the principle of
    substitutability.

25
Other Types of Inheritance
  • Construction
  • The subclass reuses a class because it provides
    needed functionality...
  • ... but it is not necessarily true that an
    instance of the subclass is an instance of the
    superclass.
  • example Javas Stack class (ouch!)
  • Construction may violate the principle of
    substitutability.
  • JUST DONT DO IT.
Write a Comment
User Comments (0)
About PowerShow.com