Professional Documents
Culture Documents
Lecture 22
Based on Slides of Dr. Norazah Yusof
1
Abstract Class
• Abstract class is a class with one or more abstract
methods.
• The abstract method
2
From Chapter 9
GeometricObject
-color: String The color of the object (default: white).
-filled: boolean Indicates whether the object is filled with a color (default: false).
-dateCreated: java.util.Date The date when the object was created.
+GeometricObject() Creates a GeometricObject.
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date Returns the dateCreated.
+toString(): String Returns a string representation of this object.
Circle Rectangle
-radius: double -width: double
+Circle() -height: double
+Circle(radius: double) +Rectangle()
+getRadius(): double +Rectangle(width: double, height: double)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void
+getArea(): double
+getPerimeter(): double 3
Abstract Classes
GeometricObject
-color: String
-filled: boolean
-dateCreated: java.util.Date
The # sign indicates
protected modifer #GeometricObject()
+getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
+getDateCreated(): java.util.Date
+toString(): String
+getArea(): double
+getPerimeter(): double
Circle Rectangle
-radius: double -width: double
protected GeometricObject(){
dateCreated = new java.util.Date();
}
:
public abstract double getArea();
public abstract double getPerimeter();
}
5
Abstract Class
public class Circle extends GeometricObject
{
private double radius;
:
:
public double getArea() {
return radius * radius * Math.PI;
}
6
Abstract Class
public class Rectangle extends GeometricObject
{
private double radius;
:
:
public double getArea() {
return width * height;
}
7
NOTE
• An abstract method cannot be contained in
a non-abstract class.
• If a subclass of an abstract superclass
does not implement all the abstract
methods, the subclass must be declared
abstract.
• In other words, if a non-abstract subclass
extended from an abstract class, all the
abstract methods must be implemented,
even if they are not used in the subclass.
8
NOTE
9
NOTE
10
NOTE
11
NOTE
12
NOTE
13
Example: Using the GeometricObject
Class
14