CSE143 Inheritance Examples handout #26
Class StutterList.java
----------------------
// Variation of ArrayList that adds values twice with the appending add.
import java.util.*;
public class StutterList extends ArrayList {
public boolean add(E value) {
super.add(value);
super.add(value);
return true;
}
}
Class MyPoint.java
------------------
// Variation of Point that keeps track of how many times each point has been
// translated and that provides a getTranslateCount method.
import java.awt.*;
public class MyPoint extends Point {
private int count;
public MyPoint() {
this(0, 0);
}
public MyPoint(int x, int y) {
super(x, y);
count = 0;
}
public void translate(int dx, int dy) {
count++;
super.translate(dx, dy);
}
public int getTranslateCount() {
return count;
}
}
Class PointTest.java
--------------------
// Class that demonstrates simple use of the MyPoint class.
public class PointTest {
public static void main(String[] args) {
MyPoint p = new MyPoint(13, 42);
p.translate(3, 14);
p.translate(72, 4);
p.translate(-8, 9);
System.out.println("Translate count = " + p.getTranslateCount());
}
}
Output of PointTest
-------------------
Translate count = 3