******************************************************************************* 0) Administrative - no homework - next 3 classes (any requests?) A4: - easy on the while(true) - go over sample solution A5 designs: - Board-- needs a 2D array as a data member * very advisable to use an initializer list * how to do it: ints or chars or images * how to draw them so they get more than 1 pixel of space - JavaMuncherUtils-- a lot like ParkDisplay * should be a data member of your main class * probably use all the methods somewhere- keep it handy - classes-- don't really need Food, could use Phantoms and Point * collective methods in Phantoms * utility methods in Point - exact collision in Point * image represented by one point- where is it? top left corner public boolean collidesExactly(Point other) { return (this.equals(other)); } * have to define an equals method - overlap detection in Point * given the point, you can find the other corners (all images same size) public boolean overlaps(Point other) { // "this"=top left of this image, "other"=top left of another // figure out bottom right pts of both: add width, height // now you have all the x,y values for both images // see if any of "these" corners are inside the other's } 1) Last time- started inheritance - arrays quiz 2) Go over 13.4-13.6 in groups 3) Highlights A. Overriding vs. overloading - method signature: name and parameter list - method prototype: signature, return type, visibility - overloading: same name, but different parameters - overriding: same signature, same return type, >= visibility B. Exercises for override/overload 1) what would override the move(dx, dy) method? overload? 2) protected int g(): in subclass, what are each of these? * private int g(int x) -- overload * public double g(String s) -- overload * protected int g() -- override * public int g() -- override * private int g() -- error * protected double g() -- error 3) do overriding example we didn't get to last time C. Constructors - not inherited, so always define one for each class - if you don't, you get a default one: public SavingsAccount() { super(); } - this calls a superclass one with no args * ok for normal classes, b/c Object has one like that * not ok for SavingsAccount, b/c BankAccount doesn't (error) * besides, it wouldn't initialize the interest rate - make ones that use calls to super(...) to set up inherited stuff * if inherited stuff is private, this is the only way * then set up additional stuff separately * call to super() has to come first - write the Actor and JavaMuncher constructors public Actor(JavaMuncherImageSequencer seq, Point start) { sequencer = seq; startLocation = start.copy(); location = start.copy(); } public JavaMuncher(JavaMuncherImageSequencer seq, Point start, ...) { super(seq, start); // initialize others } D. Interfaces - use them when you want to make a class "qualify" as something * like a KeyListener or TimerListener - in order to qualify, a class must implement certain methods * keyHit and timerEventHeard - if class implements the KeyListener interface, compiler will force it to implement them, so system can be sure it did - you can say "implements X, Y, Z, ..." on class header * can implement as many as you want * can only extend one - the difference between interfaces and inheritance * inheritance: share code b/w related classes * interfaces: enforce common usage b/w unrelated classes - the similarity: the IS-A relationship * a subclass can be put in a reference to a superclass * an implementor can be put in a reference to an interface E. Abstract classes - superclasses that aren't instantiable (don't want general kind) - declare "abstract" in header, and it's illegal to make an object - can have methods that are also abstract: no bodies - only way to use is to extend and override all abstract methods - if you don't, the subclass isn't instantiable either - kind of like an interface that way, but could also pass on code public abstract class BankAccount { ... everything the same up until ... public abstract void report(); } - Savings and Checking get almost everything they did before, but they have to override report(), and you can't make a BankAccount 4) Summary - overriding vs. overloading - constructors and inheritance - interfaces - abstract superclasses *******************************************************************************