lecture23-interfaces

lecture23-interfaces - Lecture 23 Interfaces Event...

Info iconThis 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 4 November 2009 24 November 2009 11/12/2009 CS170, Section 000, Fall 2009 1
Background image of page 1

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

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

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

View Full DocumentRight Arrow Icon
Abstract Classes and Abstract Methods GeometricObject -color: String -filled: boolean Abstract class GeometricObject.java -dat eCreat ed: java. ut il.Dat e #GeometricObject() +getColor(): String +setColor(color: String): void isFilled(): boolean The # sign indicates protected modifer Circle.java Rectangle.java +isFilled(): boolean +setFilled(filled: boolean): void +getDateCreated(): java.util.Date +toString(): String +getArea(): double getPerimeter(): double Abstract methods thd tA d tP i t idd i +getPerimeter(): double 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 +get Di am eter(): double -height: double +Rectangle() +Rectangle(width: double, height: double) +getWidth(): double +setWidth(width: double): void 4 g( ) () +getHeight(): double +setHeight(height: double): void
Background 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 { ublic String owToEat ) { public String howToEat() { return "Orange: Make orange juice"; } } 11/25/2009 CS170, Section 000, Fall 2009 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
What if we want to inherit from ultiple classes? 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”);} } lass Apricot extends Fruit{ class Apricot extends Fruit{ public void HowToEat(){ System.out.println(“Apricot: Pit it”);} } lass luot xtends Plum extends Apricot{ class Pluot extends Plum, extends Apricot{ //class code } ublic static void main (String [] rgs { 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
Background image of page 6
Interfaces What is an interface? Why is an interface useful? How do you define an interface? How do you use an interface? 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
What is an interface? hy is an interface useful? Why is an interface useful? An interface is a class-like construct that ntains only constants and abstract methods contains only constants and abstract methods Interface is to specify behavior for objects For example, we can specify that the objects are comparable, edible, cloneable using appropriate interfaces such as Comparable, Edible, and Cloneable A class that implements an interface must implement all the abstract methods For example, we can define Orange and Chicken classes that implement Edible interface 8
Background image of page 8
Interface is a Special Class Like an abstract class, you cannot create an
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/03/2010 for the course CS CS 170 taught by Professor Eugeneagichtein during the Fall '09 term at Emory.

Page1 / 65

lecture23-interfaces - Lecture 23 Interfaces Event...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online