Download presentation
Presentation is loading. Please wait.
1
Recitation 7 October 7, 2011
2
As you come in . . . Please sit next to someone with whom you will collaborate for today’s recitation. Choose someone with whom you have NOT collaborated during a previous recitation.
3
Today’s Goals: Use inheritance and overriding to specialize the behavior of variable-sized collections of graphics objects Call superclass constructors More test-first programming
4
Properties of Inheritance
All objects in Java have exactly one immediate superclass (Object if none specified) Subclasses inherit all constructors, methods, variables, and constants from their parent classes Subclasses can override methods from any superclass by defining a method with the same signature
5
Extending a class Use the extends keyword to subclass
public class AStringDatabase extends AStringHistory implements StringDatabase { public void removeElement (String element) { … } int indexOf (String element) { … } void removeElement (int index) { … } void shiftUp (int startIndex) { … } public boolean member(String element) { … } public void clear() { … } } Use the extends keyword to subclass A subclass may have additional methods and implement extra interfaces
6
Invoking a super constructor
public class AStringDatabase extends AStringHistory implements StringDatabase { boolean printWhenBeyondCapacity; // print when user tries to add to a full database public AStringDatabase(int maxCapacity, boolean printWhenPastCapacity) { super(maxCapacity); printWhenBeyondCapacity = printWhenPastCapacity; } . . . Use the super keyword to call the parent constructor Must be the first line in your constructor
7
Overriding the toString() method
public String toString() { String retVal = “”; for (int i = 0; i < size; i++) retVal += contents[i] + “:”; return retVal; } stringDatabase.toString() “James Dean:John Smith:”
8
Recitation Specification
Download Recitation7.zip from the Recitations page Implement TriangleStack by extending GeneralObjectStack TriangleStack should have: A constructor that takes no parameters and simply calls the superclass constructor A constructor that takes an integer parameter (indicating the desired maximum capacity) and calls the superclass constructor that takes an int A method called “boolean isTriangle(Object obj)” that returns true if the provided Object is an instance of (i.e. implements the interface) Triangle. Note: this method is not public – it is default. An overridden version of push(Object obj). If the provided object is a Triangle (try calling isTriangle), then the superclass’s version of push is called on obj. Your code works if you can successfully run Recitation7.java
9
Extra challenge After getting checked off for the previous part, implement: AlignedTriangleStack & CorneredTriangleStack Download Extra.zip from the Recitations page, open it, and CLICK & DRAG the files into the appropriate packages
10
CorneredTriangleStack
Create a constructor that takes no parameters and calls the superclass constructor with the constant CAPACITY Whenever a Triangle is pushed onto a non-empty stack, the x and the y of the added triangle are set to be the same as the x and the y of the top triangle on the stack. (Try to reuse as many methods as possible.)
11
AlignedTriangleStack
Create a constructor that takes no parameters and calls the superclass constructor with the constant CAPACITY Whenever a Triangle is pushed onto a non-empty stack, the x is set to be the x of the top triangle, and the y is set to be the y of the bottom of the top triangle. (Try to reuse as many methods as possible.)
12
Test extra challenge To test each part of the extra challenge, uncomment the appropriate line in main() Test each component individually before trying TransparentStackSwapperTester
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.