Lecture 5
Lecture 5
Programming II
3 – Abstract Classes and
Interfaces(1)
1
3– Abstract Classes and Interfaces -
Outline
Common Method without Common
Implementation
Case Study
2
Introduction
3
Common Method without
Common Implementation
Both Circle and Rectangle contain the getArea() and
getPerimeter() methods
The implementations of these common methods are
completely different in different classes.
There is no common implementation to be put into the
superclass.
The implementation completely depends on the specific
type of GeometricObject.
Such methods are referred to as abstract methods. They
have signatures in the superclass, but no implementation.
If a class contains abstract methods, it must be declared as
an abstract class.
4
Abstract Class and Abstract Method
GeometricObject Abstract class
-color: String
-filled: boolean
-dateCreated: java.util.Date
The # sign indicates
protected modifie r #Geo metric Object()
#Geo metric Object(color: string,
filled: boolean)
+getColor(): St ring
+setColor(colo r: String): void
+isFilled(): boolean
+setFilled(filled : boolean): void
+getDateCreated(): java.util.Date
+toString(): String
+getArea(): double
Abstract methods +getPerimeter(): double
Methods getArea and getPerimeter a re overridden in
are ita lic ized
Circ le and Rectangle. Superclass methods are generally
omitted in the UM L d iagra m for subclasses .
Circle Rectangle
-radius: double -width: double
-height: double
+Circle ()
+Circle (radius: double) +Rectangle()
6
Abstract Method
7
Abstract Method
8
Abstract Method
9
Abstract Method in Abstract
Class (Points to Remember)
An abstract class must be declared with an abstract
keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass
not to change the body of the method.
10
Abstract Method in Abstract
Class
An abstract method cannot be contained in a
nonabstract class. If a subclass of an abstract superclass
does not implement all the abstract methods, the
subclass must be defined as abstract.
In other words, in a nonabstract subclass extended from
an abstract class, all the abstract methods must be
implemented, even if they are not used in this subclass.
An abstract class cannot be instantiated using the new
operator, but you can still define its constructors, which
are invoked in the constructors of its subclasses. For
instance, the constructors of GeometricObject are
invoked in the Circle class and the Rectangle class.
11
Abstract Class without Abstract
Method
12
Superclass of Abstract Class
may be Concrete
A subclass can be abstract even if its superclass is
concrete. For example, the Object class is concrete, but
its subclasses, such as GeometricObject, may be
abstract.
A subclass can override a method from its superclass to
define it abstract. This is rare, but useful when the
implementation of the method in the superclass
becomes invalid in the subclass. In this case, the
subclass must be defined abstract.
13
Abstract Class as Type
14
Case Study: the Abstract
Number Class
java.lang.Number
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longVlaue(): long
+floatValue(): float
+doubleValue():double
15
Case Study: Taxonomy
(biology)
Eukaryota
……
Animalia
……
Chordata
……
.
.
.
Homo
……
H. sapiens
……
18
Case Study: Polymorphism
Gun
……
+fire() : void
+reload() : boolean
. . .
. . .
. . .
19
Case Study: Polymorphism
HeroCharacter
……
public pullTheTrigger() {
-theGun : Gun ……
…… if (theGun != null) theGun.fire();
……
+run() : void }
+jump() : boolean
+equipGun( gun : Gun ) : boolean
+pullTheTrigger() : boolean
20
Case Study: Polymorphism
……
neo.equipGun(new SubMachineGun(…));
neo.pullTheTrigger();
……
neo.equipGun(new Pistol(…));
neo.pullTheTrigger();
……
neo.equipGun(new SubMachineGun(…));
neo.pullTheTrigger();
……
neo.equipGun(new SubMachineGun(…));
neo.pullTheTrigger();
……
21
Case Study: Polymorphism
……
Helicopter theHelicopter = new Helicopter();
……
neo.equipGun( theHelicopter.getMinigun() );
neo.pullTheTrigger();
……
22
Case Study: Polymorphism
public abstract class Gun {
public abstract void fire();
} public class HeroCharacter {
private Gun theGun;
public class Pistol extends Gun { public boolean equipGun(Gun gun) {
private String name = “Beretta"; if(gun != null) {
public Pistol() {}; theGun = gun;
public void fire() { return true;
System.out.println("Firing a Pistol:" + name); }
} else return false;
} }
23
Case Study: Polymorphism
neo.equipGun(new SubMachineGun());
neo.pullTheTrigger();
neo.equipGun(new Pistol());
neo.pullTheTrigger();
neo.equipGun(new SubMachineGun("Skorpion"));
neo.pullTheTrigger();
neo.equipGun(new SubMachineGun("Uzi"));
neo.pullTheTrigger();
}
}
24