CS 151 ObjectOriented Design October 31 Class Meeting
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Invoking a Superclass Method o Suppose a manager’s salary is his regular employee salary plus a bonus that only managers get. public class Manager extends Employee { public double get. Salary() { return salary + bonus; // ERROR--private field } A subclass cannot access. . . private fields of its superclass. } public double get. Salary() { return get. Salary() + bonus; } SJSU Dept. of Computer Science Fall 2013: October 31 // ERROR--recursive call CS 151: Object-Oriented Design © R. Mak 2
Invoking a Superclass Method o Use the super keyword. n n Turns off polymorphism. super is not a reference. public double get. Salary() { return super. get. Salary() + bonus; } SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 3
Invoking a Superclass Constructor o Use the super keyword by itself in the subclass constructor to call the superclass constructor. n n Must be the first statement in the subclass constructor. Pass any required parameters. public Manager(String a. Name) { super(a. Name); // calls superclass constructor bonus = 0; } o If a subclass constructor doesn’t explicitly call a superclass constructor, the superclass’s default constructor (the one without parameters) is called. n n Therefore, then the superclass must have a default constructor. Otherwise, the subclass constructor must call super(). SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 4
Invoking a Class’s Own Constructor o A class can invoke its own constructor (from another constructor) by calling this: public Manager(String a. Name) { super(a. Name); // calls superclass constructor bonus = 0; } public Manager(String a. Name, double a. Bonus) { this(a. Name); // must be the first statement bonus = a. Bonus; } SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 5
Preconditions and Inheritance o Recall that a precondition of a method is a condition that must be true before the method can be called. n o The caller is responsible for making sure the precondition is true before making the call. Suppose a subclass overrides a superclass method. n The precondition of the subclass method cannot be stronger than the precondition of the overridden superclass method. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 6
Preconditions and Inheritance public class Employee { /** * Sets the employee salary to a given value. * @param a. Salary the new salary * @precondition a. Salary > 0 */ public void set. Salary(double a. Salary) {. . . } } o Suppose in Manager class’s override of set. Salary(), we set @precondition a. Salary > 100000 Employee e =. . . ; e. set. Salary(50000); n What if variable e referred to a Manager object? o Then the manager’s precondition is violated but the programmer has no way to check it when writing the code. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 7
Postconditions and Inheritance o When a subclass overrides a method, the method’s postcondition must be at least as strong as the postcondition of the superclass method. n n o Suppose Employee. set. Salary() has a postcondition that it does not decrease the employee’s salary. Then Manager. set. Salary() must have a postcondition that is at least as strong (such as increase the manager’s salary). Other conditions: n When you override a method, you cannot make it less visible. o n Example: If a superclass method is public, you cannot override the method in a subclass and make it private. An overridden method cannot throw more checked exceptions than are declared in the superclass method. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 8
Favor Composition over Inheritance o Class hierarchies should not be complex! n n o Don’t have over 5 or 6 levels. Otherwise, programmers may get lost in the hierarchy. “Has a” (aggregation) is often more flexible than “is a” (inheritance) n Use aggregation or composition to reduce the number of levels in the class hierarchy and to add flexibility. Person. With. Address Person Professor Address Student Street Email Student SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 9
Why Favor Composition Over Inheritance? o An example of the Java library getting it wrong: public class Stack<T> extends Vector<T> { T pop() {. . . } void push(T item) {. . . } n n What’s wrong with this design? Oops: Stack<String> st = new Stack<String>(); st. push("A"); st. push("B"); st. push("C"); st. remove(1); // Remove "B" in a non-stack way SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 10
Why Favor Composition Over Inheritance? o The Stack class should have used aggregation instead of inheritance: public class Stack<T> { private Vector<T> elements; T pop() {. . . } void push(T item) {. . . } n Now you can’t call remove() on a stack object. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 11
Superclasses and Interfaces «interface» Online Course CS 151 o n CS 151 CS 153 CS 160 Course is the superclass. CS 151, CS 153, and CS 160 each extends Course. CS 151, CS 153, and CS 160 each implements Online. n o CS 160 CS 151, CS 153, and CS 160 each “is a” Course. n o CS 153 Online is an interface type. What are the differences between extending a class and implementing an interface? SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 12
Differences Between Classes and Interfaces o Class (“concrete” not abstract) n n o Can contain fields. It must implement any methods that it contains. Subclasses can override the methods. A class has only one superclass (default: the Object class). Interface n n Can contain fields, but they must be constants. It does not implement any methods that it declares. Classes that implements the interface must implement each of the interface’s declared methods. A class can implement zero, one, or more interfaces. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 13
“Implements” o A class implements an interface. n o A class extends a superclass. A class implements a method by providing its code. n A Java interface type does not implement the methods that it declares. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 14
What is an Abstract Class? o An abstract class is declared with the abstract keyword. n n Example: public abstract class Food {. . . } It may or may not contain any abstract methods. o If a class has at least one abstract method, then the class itself must be declared abstract. o An abstract method is like a method of an interface. n n It has no implementation. In an abstract class, you must explicitly declare an abstract method with the abstract keyword. o o Example: public abstract void cook(. . . ); A subclass of an abstract superclass must implement each of the superclass’s abstract methods. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 15
Purpose of an Abstract Class o Unlike an interface, an abstract class can implement some or all of its methods. n o Therefore, like any other superclass, an abstract class can contain common functionality for its subclasses. An abstract class forces its subclasses to implement certain methods by declaring those methods to be abstract. n n Any subclass of the abstract class must implement each of the abstract methods. Similar: Any class that implements an interface must implement each of the interface’s methods. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 16
Important Facts about Abstract Classes o An abstract class cannot be instantiated. n n o A variable’s type can be an abstract class. n o You cannot create an object directly from an abstract class. Therefore, if you do not want a class to be instantiated, just declare it abstract even if it does not contain any abstract methods. It can refer to an object instantiated from a “concrete” subclass of the abstract class. In UML class diagrams, the name of an abstract class and the name of an abstract method is in italics. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 17
Abstract Class Example Food Abtract base class Food create. Food(int type, . . . ) void prepare() void cook() void serve() Abstract type Factory method Abstract methods Broccoli Chicken Pork. Chops void prepare() void cook() void serve() Food food = Food. create. Food(type, . . . ); food. prepare(); food. cook(); food. serve(); SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak Concrete classes Concrete object 18
Abstract Class Example Food create. Food(int type, . . . ) void prepare() void cook() void serve() o Broccoli Chicken Pork. Chops void prepare() void cook() void serve() Each subclass of the abstract Food class must implement the abstract prepare(), cook(), and serve() methods. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 19
Mouse Events o How do you get a Swing component such as a panel to respond to mouse events? n Mouse clicks o o n Mouse motion o o o n which button x and y coordinates of the click current x and y coordinates of the mouse pointer drags x and y coordinates of where the mouse entered and exited Mouse wheel o wheel moved _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 20
Mouse Events o The panel wants to listen to mouse events. n n n Mouse event listener Event Method enter mouse. Entered(Mouse. Event e) exit mouse. Exited(Mouse. Event e) button pressed mouse. Pressed(Mouse. Event e) button released mouse. Released(Mouse. Event e) button clicked mouse. Clicked(Mouse. Event e) Mouse motion listener Event Method move mouse. Moved(Mouse. Event e) drag mouse. Dragged(Mouse. Event e) Mouse wheel listener Event Method wheel moved mouse. Wheel. Moved(Mouse. Wheel. Event e) SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 21
Mouse Adapter o o Interfaces Mouse. Event. Listener, Mouse. Motion. Listener, and Mouse. Wheel. Listener together declare a number of methods. What if you’re only interested in a few mouse events, say only mouse pressed and mouse dragged? n o Abstract class Mouse. Adapter implements all three interfaces and therefore it implements all their methods. n o Do you still have to implement all the other methods? However, it implements each method to do nothing. Create a subclass of Mouse. Adapter and only override the methods you care about. n The remaining methods will inherit doing nothing. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 22
Mouse Adapter public abstract class Mouse. Adapter implements Mouse. Listener, Mouse. Motion. Listener, Mouse. Wheel. Listener { // Mouse event listener public void mouse. Clicked(Mouse. Event event) {} public void mouse. Pressed(Mouse. Event event) {} public void mouse. Released(Mouse. Event event) {} public void mouse. Entered(Mouse. Event event) {} public void mouse. Exited(Mouse. Event event) {} Do nothing! // Mouse motion listener mouse. Moved(Mouse. Event e) {} mouse. Dragged(Mouse. Event e) {} // Mouse wheel listener mouse. Wheel. Moved(Mouse. Wheel. Event e) {} } SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 23
Mouse Adapter o Suppose we have a JPanel object that’s only interested in listening to mouse button press and a mouse drag events. n Create two anonymous classes that each extends the Mouse. Adapter abstract class. n Override the mouse. Pressed() and mouse. Dragged() methods. o Each overrides the default “do nothing” behavior in Mouse. Adapter in order to do something useful for the application. _ SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 24
Mouse Adapter mouse. Panel. add. Mouse. Listener(new Mouse. Adapter() { public void mouse. Pressed(Mouse. Event event) { event. Label. set. Text("Pressed"); button. Label. set. Text(BUTTONS[event. get. Button()]); count. Label. set. Text(String. value. Of(event. get. Click. Count())); x. Label. set. Text(String. value. Of(event. get. X())); y. Label. set. Text(String. value. Of(event. get. Y())); } } ); SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 25
Mouse Adapter mouse. Panel. add. Mouse. Motion. Listener(new Mouse. Adapter() { public void mouse. Dragged(Mouse. Event event) { event. Label. set. Text("Dragged"); count. Label. set. Text(String. value. Of(event. get. Click. Count())); x. Label. set. Text(String. value. Of(event. get. X())); y. Label. set. Text(String. value. Of(event. get. Y())); } } ); Demo SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 26
Car Dragging Demo o Recall our original car demo that animated a car using a timer. n n o We embedded drawing a car shape inside the paint. Icon() method of a subclass of Icon. For this new demo, we’ll embed drawing a car shape in the paint. Component() method of a subclass of JComponent. The advantage of using a subclass of JComponent is that we inherit all the features of that superclass. n n In particular, our JComponent subclass will inherit listening to mouse events. Events we are interested in: o o n mouse button pressed mouse dragged Therefore, we will use the abstract Mouse. Adapter class. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 27
Car Dragging Demo o The application will allow us to use the mouse to drag a car shape within a frame. Demo add. Mouse. Listener(new Mouse. Adapter() { public void mouse. Pressed(Mouse. Event event) { mouse. Point = event. get. Point(); if (!car. contains(mouse. Point)) { mouse. Point = null; } } } ); n Store the point where the mouse button went down inside the car shape in field mouse. Point. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 28
Car Dragging Demo add. Mouse. Motion. Listener(new Mouse. Motion. Adapter() { public void mouse. Dragged(Mouse. Event event) { if (mouse. Point == null) return; Point last. Mouse. Point = mouse. Point; mouse. Point = event. get. Point(); double dx = mouse. Point. get. X() - last. Mouse. Point. get. X(); double dy = mouse. Point. get. Y() - last. Mouse. Point. get. Y(); car. translate((int) dx, (int) dy); repaint(); } } ); n Drag the car by tracking the mouse. SJSU Dept. of Computer Science Fall 2013: October 31 CS 151: Object-Oriented Design © R. Mak 29
- Slides: 29