您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Apply Index

Apply Page # 12

Exercise 9 — Using abstract methods to generalise the shape applet program

Refer back to Activity 1 — MyCircle and MySquare extensions of MyShape.

We can generalise the MyShape class by providing an abstract draw() method for the classes MyCircle and MySquare to override.

The advantage of this means that a MyShape variable will be able to refer to an object that is from any subclass of MyShape, and we can send that variable draw() messages. The message draw() will be in the protocol for MyShape since it is defined as an abstract class.

So:

  • define an abstract method draw() in the class MyShape
  • change your ShapeApplet class to use a single variable shape of type MyShape
  • to first have shape refer to a new circle object (as before change its x position and then make it draw itself)
  • then have the shape varaible refer to a new square object (and as before change its y position and make it draw itself)

Discussion of Exercise 9
Back to content

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg