Sei sulla pagina 1di 6

Announcements

Due date//me of Long Assignment 5 extended to tomorrow, Wednesday 23/Nov, at 5pm Long Assignment 6 will go out tomorrow, due next Tuesday 29/Nov Long Assignment 7 will go out Tuesday 29/Nov, due Tuesday 6/Dec (last day of class) Quiz 9 (10 points) this Thursday, 24/Nov Quiz 10 (20 points) next Thursday, 1/Dec

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Announcements
Today Introduc/on to Polymorphism (Chapter 8) Tomorrow Introduc/on to Inheritance (Chapter 8) Friday Introduc/on to File I/O and Excep/ons (Brief excerpts from Chapters 9 & 10) Next week Introduc/on to GUIs and Java Swing (Brief excerpts from Graphics Supplements of various Chapters) Following Tuesday (last day of class) Wrap-up & Presenta/on by Kobla about his experience working at Google.

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Polymorphism

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Java Interfaces & Polymorphism


Relevant reading: Sec/on 8.1 of Textbook A Java interface is a program component that contains the headings for a number of public methods. Objects having the same interface can be used interchangeably This ability to subs/tute one object for another is called polymorphism.
G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 4

Java Interfaces - Example


Lets look at an example to do with transporta/on
See the Transporta/onOp/on.java which declares an interface containing methods that all transporta/on op/ons should implement See Car.java, Bicycle.java, Horse.java which are classes represen/ng specic transporta/on op/ons. They all implement the Transporta/onOp/on interface and hence provide deni/ons for the methods in this interface. See TestTransporta/on.java and TestTransporta/on2.java which demonstrate the use of the Transporta/onOp/on interface and the classes that implement it.

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Ac/vity
Download the provided code: Drawable interface, Rectangle class (this implements the Drawable Interface) and Picture class (this is an Applet) Do the following: Implement the init() method of the Picture class
Add code to the init() method of the Picture class to create an array of Drawable items Instan/ate 2 or more Rectangle objects and add them to the array

Implement the paint() method of the Picture class


Write a for loop that loops through the array of Drawable objects and draws those objects by calling the drawWithGraphics() method

Create a new class of your choice (e.g. a Circle/Triangle/Pyramid/Cup) that also implements Drawable Modify the init method to add objects of this new class to the array (in addi/on to the Rectangles). You should no/ce that the paint() method now draws both types of objects
G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 6

Potrebbero piacerti anche