lecture23-interfaces

lecture23-interfaces - Lecture 23 Interfaces Event...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 23: Interfaces; Event Programming CS 170, Section 000 24 November 2009 11/12/2009 CS170, Section 000, Fall 2009 1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture Plan Homework 7 questions? ¾ Multiple inheritance: interfaces (parts of ch. 11) Example Event programming (parts of ch 15) Example: Event programming (parts of ch. 15) ¾ Exceptions (chapter 18) CS170, Section 000, Fall 2009 2
Image of page 2
Recall: Superclasses and Subclasses GeometricObject1.java Circle4.java Rectangle1.java 3
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Abstract Classes and Abstract Methods GeometricObject -color: String -filled: boolean Abstract class GeometricObject.java Ci l j -dateCreated: java.util.Date #GeometricObject() +getColor(): String +setColor(color: String): void +isFilled(): boolean The # sign indicates protected modifer Circle.java Rectangle.java +setFilled(filled: boolean): void +getDateCreated(): java.util.Date +toString(): String +getArea(): double +getPerimeter(): double Abstract methods M th d tA d tP i t idd i Circle -radius: double Rectangle -width: double Methods getArea and getPerimeter are overridden in Circle and Rectangle. Overridden methods are generally omitted in the UML diagram for subclasses. +Circle() +Circle(radius: double) +getRadius(): double +setRadius(radius: double): void +getDiameter(): double -height: double +Rectangle() +Rectangle(width: double, height: double) +getWidth(): double +setWidth(width: double): void 4 +getHeight(): double +setHeight(height: double): void
Image of page 4
Fruit Example Revisited abstract class Fruit { // Data fields, constructors, and methods omitted here } class Apple extends Fruit { { public String howToEat() { return "Apple: Make apple cider"; } } class Orange extends Fruit { public String howToEat() { return "Orange: Make orange juice"; } } 11/25/2009 CS170, Section 000, Fall 2009 5
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What if we want to inherit from multiple classes? Example: Pluot (hybrid of plum and apricot) l Pl t d F it{ class Plum extends Fruit{ public void HowToEat(){ System.out.println(“Plum: Wash”);} } class Apricot extends Fruit{ public void HowToEat(){ System.out.println(“Apricot: Pit it”);} } class Pluot extends Plum extends Apricot{ class Pluot extends Plum, extends Apricot{ //class code } public static void main (String [] args){ Pluot p = new Pluot(); p.HowToEat(); //which HowToEat method will be called? } } 11/25/2009 CS170, Section 000, Fall 2009 6
Image of page 6