Download presentation
Presentation is loading. Please wait.
1
unit 9 1 Unit 9: Enhanced class design H In this unit: Abstract classes Packages basic programming concepts object oriented programming topics in computer science syllabus
2
unit 9 2 Abstract Classes H An abstract class is a placeholder in a class hierarchy that represents a generic concept H An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract H An abstract class often contains abstract methods (like an interface does), though it doesn’t have to
3
unit 9 3 Abstract Classes H The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract H An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) H The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate
4
unit 9 4 Abstract classes: example Paint Brush Paint Brush application
5
unit 9 5 Paint-brush application We want to represent the drawing as a collection of objects, each represents a certain shape We want to represent each shape with a suitable class; objects of these classes will know how to draw themselves The classes have common properties (location, size, color,...) and common behaviors (moveTo(), resize(),...) We want to have a common superclass for all these classes which will define all the common properties and behaviors
6
unit 9 6 Common superclass Shape Shape Rectangle Ellipse moveTo(x,y) moveBy(dx,dy) resize(factor) setLineColor(color)... draw() contains(x,y) draw() contains(x,y)
7
unit 9 7 Class shape is Abstract shape H Class shape was defined to ease our design and maintenance by putting all the common properties of the different shapes in one place H However, we will never want to create instances of class shape, only of its subclasses H We thus define the class shape as an abstract class; now we cannot create instances of class shape, only instances of subclasses of shape which are not defined abstract
8
unit 9 8 Class Shape: definition // A geometrical shape. A shape has a location and size, // and it can draw itself on the screen... Shape public abstract class Shape { // The top-left corner of the rectangular area that // contains the shape protected int x,y; // The dimension of the shape (of the rectangular // area that contains the shape) protected int width, height; // The color of the border of the shape private Color lineColor; //... other properties
9
unit 9 9 Class Shape: methods // Construct new shape with given location & dimension public Shape(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } // Return the color of the border of this shape public Color getLineColor() { return lineColor; } public void moveTo(int x, int y) {// Move to new location this.x = x; this.y = y; } public void moveBy(int dx, int dy) {//Move by given offset moveTo(x+dx, y+dy); }
10
unit 9 10 Why abstract methods Problem: in the design diagram, the draw() method appears in the interface of all subclasses of class shape, but is implemented differently in each one of them H Solution: we define an abstract method in class shape to require that every shape can draw itself; we then override this method in every specific subclass H Polymorphism: we can draw an heterogeneous collection of shapes with a single loop
11
unit 9 11 Abstract methods of class Shape // A geometrical shape.... Shape public abstract class Shape {... state variables as before // Draws this shape public abstract void draw(); // Checks a given point is in the // interior of this shape public abstract boolean contains(int x, int y);
12
unit 9 12 Using abstract classes // A picture made of geometrical shapes PaintBrushPicture public class PaintBrushPicture { private Vector shapes; // Construct an empty picture public PaintBrushPicture() { shapes = new Vector(); } // Add a new shape to the picture public void add(shape shape) { shapes.addElement(shape); } // Paint this picture public void draw() { for (int i=0; i<shapes.size(); i++) { ((Shape)shapes.elementAt(i)).draw(); } }
13
unit 9 13 Abstract classes hierarchy H Now, a subclass of class shape must either be abstract or implement all the abstract classes that are defined in class shape
14
unit 9 14 Subclass Rectangle // A shape of a rectangle in the plane Rectangle public class Rectangle extends shape { // Constructs new Rectangle with location & dimensions public Rectangle(int x, int y, int width, int height){ super(x, y, width, height); } // Draws this rectangle public void draw() { Brush painter = new Brush(); painter.setLocation(x,y); painter.moveForwards(width); painter.turnRight(90); painter.moveForwards(height); painter.turnRight(90); painter.moveForwards(width); painter.turnRight(90); painter.moveForwards(height); }
15
unit 9 15 Chess example ChessPiece Pawn Rook moveTo(x,y) moveBy(dx,dy) getLocation() getPossibleMoves()... getPossibleMoves() Knight...
16
unit 9 16 Logic circuits example ElectronicGate AndGate OrGate getNumberOfInputs() getInputVoltage(i) setInputVoltage(i) getOutputVoltage() process() NotGate...
17
unit 9 17 More complex structures: interface Paint Brush Paint Brush application Suppose we want to paint also pixels and lines:
18
unit 9 18 No common superclass exists Object shape Line Point Rectangle Ellipse Polygon RoundRectangle We don’t have a common viewpoint of these classes that sees the method draw Pixel
19
unit 9 19 Solution: interface Object shape Line Point Rectangle Ellipse Polygon RoundRectangle Drawable Pixel
20
unit 9 20 Interface Drawable // An interface for classes whose instances know how to // draw themselves on a graphical window Drawable public interface Drawable { // Draw this object. public void draw(); } // Represents a pixel on a graphical area Pixel public class Pixel extends Point implements Drawable {... // Draw this pixel on the screen public void draw() {... }...
21
unit 9 21 Abstract classes & interfaces // A geometrical shape. A shape has a location and // size it can draw itself on the screen... shape public abstract class shape implements Drawable { // The top-left corner of the rectangular area // that contains the shape protected int x,y;... we do not have to declare again the abstract method draw()
22
unit 9 22 PaintBrushPicture example // A picture made of geometrical shapes PaintBrushPicture public class PaintBrushPicture { // The elements (shapes) that compose this picture private Vector elements;... // Draw this picture public void draw() { for (int i=0; i< elements.size(); i++) { ((Drawable)elements[i]).draw(); } }
23
unit 9 23 Visualization RoundRectagle getLocation() moveTo() moveBy() draw() toString()... getArcSize() setArcSize() Object view shape view RoundRectangle Drawable view
24
unit 9 24 Interface Moveable // An interface for classes whose instances can move Moveable public interface Moveable { // Moves this object to a new location public void moveTo(int x, int y); // Moves this object by a given offset public void moveBy(int dx, int dy); }
25
unit 9 25 Abstract classes and multiple interfaces // A geometrical shape: a shape has a location and // size it can draw itself on the screen, and move public abstract class shape implements Drawable, Moveable{... // Moves this shape to a new location public void moveTo(int x, int y) { this.x = x; this.y = y; }...
26
unit 9 26 Unit 9: Enhanced class design Abstract classes Packages
27
unit 9 27 Packages H Java lets us group our classes into packages H When we define classes we often define them as part of a package of related classes H To group several classes into a package we must: 1.Specify in each of the source files of the class that the class belongs to the package 2.Put all class files under a directory with the name of the package; the directory that contains this directory should be in our CLASSPATH
28
unit 9 28 Packages package shapes; // A geometrical shape. A shape has a location and // size shape public abstract class shape { // The top-left corner of the rectangular area // that contains the shape protected int x,y;...
29
unit 9 29 Packages package shapes; // A shape of a rectangle... Rectangle public class Rectangle extends shape { // Constructs a new Rectangle with given // location and dimenstions public Rectangle(int x, int y, int width, int height){ super(x, y, width, height); }...
30
unit 9 30 Packages package paintbrush; import shapes.*; import java.util.Vector; import java.awt.*; // A canvas on which you can draw geometrical shapes PaintBrushCanvas public class PaintBrushCanvas extends Canvas { private PaintBrushPicture picture; // Construct a new PaintBrushCanvas public PaintBrushCanvas() { picture = new PaintBrushPicture(); }
31
unit 9 31 Packages shapes shape.class Rectangle.class Ellipse.class Polygon.class paintbrush PaintBrushCanvas.class PaintBrushPicture.class PaintBrush.class my_classes (c:)
32
unit 9 32 Reusability: the Java Library H Java supplies hundreds of predefined classes, for representing commonly used object types. In addition the programmer can define his/her own new classes H The Java API (Application Programmer Interface) is the library of classes supplied by Java H The classes in the Java API are separated into packages H Each package contains a set of classes that are related in some way
33
unit 9 33 The Java Packages java.applet java.awt java.beans java.io java.lang java.math... java.net java.rmi java.security java.sql java.text java.util
34
unit 9 34 Importing Packages H Using a class from a Java package can be accomplished by using its fully qualified name: java.util.Random random = new java.util.Random(); H Instead, the class can be imported once with the import statement: import java.util.Random;... Random random = new Random();
35
unit 9 35 Importing Packages H You can also import all the classes in a given package with a single import statement: import java.util.*; The java.lang package is automatically imported into every Java program.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.