21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER<br />

15 Object-oriented<br />

programming<br />

This chapter:<br />

■ explains how encapsulation, inheritance and polymorphism are provided in a<br />

programming language<br />

■ explains the role of libraries in object-oriented programming<br />

■ explains the idea of generics or templates<br />

■ explains provision <strong>for</strong> dynamic data structures and garbage collection.<br />

15.1 ● Introduction<br />

Most current mainstream languages embody OOP. The three pillars of OOP are encapsulation,<br />

inheritance and polymorphism. This chapter explains how the programming<br />

language can support these concepts. The language used <strong>for</strong> illustration is Java, and the<br />

case study is the cyberspace invaders game (Appendix A).<br />

We go on to explain how classes can be made more general using the concept of<br />

generics, sometimes termed templates.<br />

Next we discuss approaches to creating dynamic data structure – structures that<br />

expand and contract as necessary. In particular, we look at using pointers.<br />

Finally we explore the challenge of garbage collection – what happens to computer<br />

memory after it has been used as part of a dynamic data structure.<br />

15.2 ● Encapsulation<br />

Encapsulation means bringing together data and actions that are related. In OOP such<br />

a collection is called a class. It consists of some related variable declarations and some<br />

methods. A class is a template or blueprint <strong>for</strong> any number of objects that can be created<br />

from it. In many programming languages, including Java, this is accomplished<br />

using the key word new. Creating an object is also known as instantiating an object.<br />

We will use the example of the cyberspace invaders game, Figure 15.1, to illustrate<br />

encapsulation. The program displays a number of graphical objects at various times on

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!