Abstract Classes
Object-Oriented Programming
Outline
■ Abstract classes
■ Abstract methods
■ Design pattern: Template method
■ Readings:
❑ HFJ: Ch. 8.
Đại học Công nghệ - ĐHQG HN Abstract classes 2
Our previous design
Dog d = new Dog();
Cat c = new Cat();
Fine. But…
Animal anim = new Animal();
What does an Animal look like?
Đại học Công nghệ - ĐHQG HN Abstract classes 3
What does an Animal look like?
■ What does a new Animal() object look like?
■ What are the instance variable values?
■ What should makeNoise(), eat(), and roam() do?
■ Do we ever need an Animal object?
Đại học Công nghệ - ĐHQG HN Abstract classes 4
What does a Shape look like?
■ What does a generic Shape object look like?
■ How to draw() it?
Shape
- x, y
■ Do we ever need a Shape object? + draw()
+ erase()
Point Circle Rectangle
- radius - width
+ draw() - height
+ draw()
+ erase() + erase() + draw()
+ erase()
Đại học Công nghệ - ĐHQG HN Abstract classes 5
Abstract classes
■ Some classes just should not be instantiated!
■ We want Circle and Triangle objects, but no Shape objects.
We want Dogs and Cats, but no Animal objects…
■ Make those generic classes abstract classes
abstract class Animal { ... }
❑ The compiler will guarantee that no instances of abstract
classes are created.
❑ But object references of abstract class types are allowed.
Animal a = new Animal(); // Error!!!
Animal anim = new Dog(); // no error.
Đại học Công nghệ - ĐHQG HN Abstract classes 6
abstract public class Animal {
public void eat() {}
... This is OK.
} You can always assign a subclass
object to a super class reference,
-----------------------------------------------
even if the superclass is abstract.
public class CreateAnimals {
public void go() {
Animal a;
class Animal is marked abstract,
a = new Hippo();
so the compiler will NOT let you
a = new Animal();
do create an instance of Animal.
[Link]();
}
} % javac [Link]
[Link][Link] Animal is abstract;
cannot be instantiated
a = new Animal();
^
1 error
Đại học Công nghệ - ĐHQG HN Abstract classes 7
Abstract vs. Concrete
■ A class that is not abstract
is called a concrete class
How do we know
when a class should
be abstract?
Đại học Công nghệ - ĐHQG HN Abstract classes 8
Abstract methods
■ How do we implement? public void makeNoise() {
❑ [Link](), eat()… [Link]("Hmm");
}
❑ We can't think of
a generic implementation that is useful
■ So, we mark those methods abstract.
■ Abstract methods has no body.
abstract public class Animal { No method body!
End it with a semicolon.
public abstract void makeNoise();
...
Đại học Công nghệ - ĐHQG HN Abstract classes 9
Abstract methods
■ If you declared a method abstract,
you must mark the class abstract, as well. You can't
have a concrete class with an abstract method.
■ An abstract class means that it must be extended.
■ An abstract method means that it must be overriden.
■ A concrete subclass must have all the inherited
abstract methods implemented.
Đại học Công nghệ - ĐHQG HN Abstract classes 10
abstract public class Shape {
protected int x, y;
Shape(int _x, int _y) {
x = _x;
y = _y;
}
abstract public void draw();
abstract public void erase();
public void moveTo(int _x, int _y) {
erase();
x = _x; public class Circle extends Shape {
y = _y; private int radius;
draw(); public Circle(int _x, int _y, int _r) {
} super(_x, _y);
} radius = _r;
}
public void draw() {
[Link]("Draw circle at "+x+","+y);
}
public void erase() {
[Link]("Erase circle at "+x+","+y);
}
}
Đại học Công nghệ - ĐHQG HN Abstract classes 11
Design pattern: Template method
12
Design pattern: Template method
abstract class Shape {
protected int x, y;
public void moveTo(int x1, int y1) {
erase();
x = x1;
y = y1;
draw();
}
abstract public void erase();
abstract public void draw();
}
Đại học Công nghệ - ĐHQG HN Abstract classes 13
Abstract super class
■ As a super class
❑ A common superclass for several subclasses
❑ Factor up common behavior
❑ Define the methods all the subclasses respond to
■ As an abstract class
❑ Force concrete subclasses to override methods that are
declared as abstract in the super class
■ Circle, Triangle must implement their own draw() and erase()
❑ Forbid creation of instances of the abstract superclass
■ Shape objects are not allowed
Đại học Công nghệ - ĐHQG HN Abstract classes 14
Account example
■ Problem details:
❑ You need to store information for bank accounts
❑ Assume that you only need to store the current balance, and the
total number of transactions for each account.
❑ The goal for the problem
Whichisclasses
to avoidand
duplicating
methodscode between the
three types of account. should be abstract?
❑ An account needs to respond to the following messages:
■ constructor(initialBalance)
■ deposit(amount) : put money to the account
■ withdraw(amount): get money from the account
■ endMonth(): to run at the end of each month to conclude the month:
Apply the end-of-month charge; print out a summary; zero the transaction
count.
Đại học Công nghệ - ĐHQG HN Abstract classes 15