0% found this document useful (0 votes)
25 views

Lecture 5

The document discusses abstract classes and interfaces in object-oriented programming. It provides examples of abstract classes like GeometricObject that cannot be instantiated directly but provide common properties and methods for subclasses like Circle and Rectangle. It also describes how abstract methods define a method signature in the superclass but leave the implementation to subclasses.

Uploaded by

Waley Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture 5

The document discusses abstract classes and interfaces in object-oriented programming. It provides examples of abstract classes like GeometricObject that cannot be instantiated directly but provide common properties and methods for subclasses like Circle and Rectangle. It also describes how abstract methods define a method signature in the superclass but leave the implementation to subclasses.

Uploaded by

Waley Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

COMP 212 –

Programming II
3 – Abstract Classes and
Interfaces(1)

1
3– Abstract Classes and Interfaces -
Outline
Common Method without Common
Implementation

Abstract Class and Abstract


Method

Abstract Class as Type

Case Study

2
Introduction

 Class design should ensure a superclass contains


common features of its subclasses.
 If you move from a subclass back up to a superclass, the
classes become more general and less specific.
 Sometimes, a superclass is so abstract it cannot be used
to create any specific instances. Such a class is referred
to as an abstract class.

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()

+Circle (radius: double, color: string, +Rectangle(width: double, height: double)


filled: boolean) +Rectangle(width: double, height: double,
+getRadius(): double color: string, filled: boolean)

+setRadius(radius: double): void +getWidth(): double

+getDia meter(): double +setWidth(width: double): void


+getHeight(): double 5
+setHeight(height: double): void
Abstract Class and Abstract
Method
 Abstract classes are like regular classes, but you cannot
create instances of abstract classes using the new
operator.
 An abstract method is defined without implementation.
Its implementation is provided by the subclasses.
 A class that contains abstract methods must be defined
as abstract.

6
Abstract Method

 Why Abstract Methods?


 You may be wondering what advantage is gained by
defining the methods getArea() and getPerimeter() as
abstract in the GeometricObject class.
 Note that you could not define the equalArea() method for
comparing whether two geometric objects have the same
area if the getArea() method were not defined in
GeometricObject.
 Think about it: what is an instance of an GeometricObject?
 Now you have seen the benefits of defining the abstract
methods in GeometricObject.

7
Abstract Method

 Abstract methods capture the function, not the


implementation.
 Abstract methods are placeholders that are meant to be
overridden.
 Thus, we don’t have private or static abstract methods.

8
Abstract Method

 Abstract methods allow us to write code that makes use


of a function without knowing the implementation, this
helps to partially specify framework.

public abstract class GeometricObject { ...


public abstract double getArea();
public static double getTotalArea(GeometricObject[] ss) {
double ta = 0.0;
for ( GeometricObject s : ss ) ta += s.getArea();
return ta;
}

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

 A class that contains abstract methods must be


abstract. However, it is possible to define an abstract
class that doesn’t contain any abstract methods.
 In this case, you cannot create instances of the class
using the new operator.
 This abstract class is used as a base class for defining
subclasses.

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

 You cannot create an instance from an abstract class


using the new operator, but an abstract class can be
used as a data type. Therefore, the following
statement, which creates an array whose elements are
of the GeometricObject type, is correct:
GeometricObject[] objects = new GeometricObject[10];
 You can then create an instance of GeometricObject
and assign its reference to the array like this:
objects[0] = new Circle();

14
Case Study: the Abstract
Number Class

java.lang.Number
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longVlaue(): long
+floatValue(): float
+doubleValue():double

Double Float Long Integer Short Byte BigInteger BigDecimal

15
Case Study: Taxonomy
(biology)
Eukaryota
……

Animalia
……

Chordata
……
.
.
.

Homo
……

H. sapiens
……

By Peter Halasz. (User:Pengo) - Own work, Public Domain,


https://commons.wikimedia.org/w/index.php?curid=2480732 16
Case Study: Polymorphism

Lots of (different kinds of) guns

18
Case Study: Polymorphism

Gun
……
+fire() : void
+reload() : boolean

Pistol Rifle SubMachineGun


……
…… …… ……

. . .
. . .
. . .

19
Case Study: Polymorphism

HeroCharacter
……
public pullTheTrigger() {
-theGun : Gun ……
…… if (theGun != null) theGun.fire();
……
+run() : void }
+jump() : boolean
+equipGun( gun : Gun ) : boolean
+pullTheTrigger() : boolean

HeroCharater neo = new HeroCharater();

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;
} }

public class SubMachineGun extends Gun { public boolean pullTheTrigger() {


private String name = "MP5"; if(theGun != null) {
public SubMachineGun() {}; theGun.fire();
public SubMachineGun(String name) { return true;
this.name = name; }
} else return false;
public void fire() { }
System.out.println("Firing a SubMachineGun:" + name); }
}
}

23
Case Study: Polymorphism

public class TestWorld {


public static void main(String[] args) {
HeroCharacter neo = new HeroCharacter();

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

You might also like