Aroma 3: Class Hierarchy, Inheritance and Polymorphism - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Aroma 3: Class Hierarchy, Inheritance and Polymorphism

Description:

Aroma 3: Class Hierarchy, Inheritance and Polymorphism. Special Topics: Java. Dr. Tim Margush ... The package statement indicates that this class will be part ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 18
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: Aroma 3: Class Hierarchy, Inheritance and Polymorphism


1
Aroma 3 Class Hierarchy, Inheritance and
Polymorphism
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
Counter Package
  • package Counters
  • The package statement indicates that this class
    will be part of a package named Counters.
  • A package is a collection of closely related
    class files. All files in the Counters package
    must be stored in a directory named Counters.

3
Counter Root Class
  • Abstract class
  • can not be used to instantiate objects
  • contains methods that must be implemented in
    subclasses
  • Public scope
  • clients know about Counters
  • Instance variables
  • initialCount
  • theCount
  • Methods
  • constructors
  • count()
  • reset()
  • setCount(int)
  • int getCount()

4
Instance Variables
  • abstract public class Counter
  • protected int theCount
  • private int initialCount
  • Subclasses will contain these variables
  • can directly access theCount
  • can not directly access initialCount
  • Clients have no knowledge of these

5
Constructors
  • Need to specify an initial counter value
  • protected Counter(int initialCount)
  • theCount this.initialCount initialCount
  • Default constructor
  • protected Counter()
  • this(0) //indirecting call

6
Methods
  • public int getCount()
  • return theCount
  • protected void setCount(int setTo)
  • theCount setTo
  • public void reset()
  • setCount(initialValue)
  • abstract public void count()

7
A Limited Counter Class
  • A counter with built-in counting limit
  • abstract class LimitedCounter
  • extends Counter
  • protected int limitValue
  • protected LimitedCounter(int initialValue, int
    limit)
  • super(initialValue) limitValuelimit
  • public boolean isAtLimit()
  • return theCountlimitValue

8
LimitedCounter is Abstract
  • There is still no implementation for count()
  • should we count up or count down?
  • what should we count by? 1, 2, ...
  • what happens if count() called at limit?
  • exception
  • rollover
  • ignore

9
Real Simple Counters
  • UpCounter, DownCounter
  • public subclasses of Counter
  • public class UpCounter extends Counter
  • public UpCounter(int initialValue)
  • super(initialValue)
  • public void count()
  • theCount

10
Real Limited Counters
  • LimitedUp, LimitedDown
  • public class LimitedDown
  • extends LimitedCounter
  • public LimitedDown(int initial, int lowLim)
  • super(initial, lowLimltinitial?lowLiminitial)
  • public void count()
  • if theCountgtlimitValue) theCount--

11
Real Rollover Counters
  • Rollover, RollUnder
  • public class Rollover
  • extends LimitedCounter
  • public Rollover(int initial, int hiLim)
  • super(initial, hiLimgtinitial?hiLiminitial)
  • public void count()
  • if theCountltlimitValue) theCount
  • else reset()

12
Warning Counter
  • Public class WarningCounter extends LimitedUp
  • Overrides LimitedUp count method to throw an
    exception if you try to count past the limit
  • throw new CounterException("Attempt to count past
    limit!")

13
CounterException Class
  • package Counters
  • class CounterException extends RuntimeException
  • public CounterException( String reason )
  • super( reason ) // end constructor
  • //end class

14
LimitedUpSkip Counter
  • public class LimitedUpSkip extends LimitedUp
  • private int skipValue
  • public LimitedUpSkip ()
  • this(0,20,5) // end default constructor
  • public LimitedUpSkip ( int initial, int max, int
    skip )
  • super( initial, max ) skipValueskipgt0?skip1
  • public void count()
  • for (int i1 iltskipValue i) super.count()

15
UpSetCounter Class
  • public class UpSetCounter extends UpCounter
  • public void setCount(int newCount)
  • super.setCount(newCount)
  • This exposes setCount as public, making it
    available to clients
  • setCount was defined in Counter
  • access has been relaxed to public

16
UpDownCounter Class
  • Not part of Counters package
  • public class UpDownCounter extends
    Counters.UpCounter
  • public UpDownCounter(int initialValue)
  • super(initialValue) //fails if package scope
    in UpCounter
  • public void unCount()
  • theCount-- //fails if package scope

17
The Counter Hierarchy
Counter
UpCounter
UpDownCounter
UpSetCounter
LimitedUp
LimitedCounter
Abstract Classes
WarningCounter
LimitedUpSkip
Package Classes
RollOver
Write a Comment
User Comments (0)
About PowerShow.com