1 W h e n I look at objects that seem to be the same sort of thing but have quite different values, how do I k n o w w h e t h e r t h e y should be modelled as different classes or as different objects of the same class? For example, if I have a father, m o t h e r and sev- eral children, should I create a new class for each or are t h e y all of objects of a general class like Person?
It depends on w h a t y o u r system needs to record about these objects and w h a t it wants t h e m to do. In a library system t h e y w o u l d all just be objects of a class such as FamilyMember. In a school system the children w o u l d all be objects of the Student class and the parents might just feature as attributes. However, if you were writing a system that simulates the behaviour of families you might model t h e m as separate classes. For entities to w a r r a n t being modelled as different classes, t h e y must have distinguishing features, i.e. distinctive behaviour or attributes, not just different values for their attributes.
2 I have two classes, Customer and Employee. Both Customer and Employee need to record title, forename and surname; can I make them subclasses of a class Name so that t h e y inherit name details, as in Figure 4.33-
This would be a totally incorrect use of inheritance. Customer and Employee should not be modelled as specializations of Name as there is not an is-a relationship between them; neither customers nor employees are kinds of name. A better way to model this would be to use an association relationship as shown in Figure 4.34-
Customer
I
cust#telephone#
address
Name title forename surname
Employee
I
employee#extension#
payroll#
grade Figure 4.33 Incorrect inheritance from Name class
OBJECTS AND CLASSES: THE BASIC CONCEPTS COMMON PROBLEMS
109
Employee employee#
extension#
payroll#
grade
Customer cust#
telephone#
address
Name title forename surname
k~
~ c a l l e dFigure 4.34 Improved model of the Name example
W h e n I am dealing w i t h an inheritance hierarchy, I sometimes can't tell w h e t h e r one class should be a subclass of another or if I am looking at two members of the same class. For example, is Shetland p o n y a subclass of Horse?
For something to qualify as a subclass it must have at least one extra a t t r i b u t e or operation or over-ride an inherited operation.
Organizing classes into an inheritance hierarchy seems like a lot of extra work; is it always w o r t h doing?
Organizing classes into an inheritance hierarchy does involve extra effort. It is w o r t h doing if it simplifies y o u r model and clarifies y o u r thinking. It is also w o r t h w h i l e if it seems quite likely that you will need to introduce n e w classes in the future and an inheritance hierarchy w o u l d simplify the process.
A n o t h e r factor is w h e t h e r you are t h i n k i n g of reusing any of y o u r classes. The more general t h e y are, the easier t h e y are to reuse.
5 Do subclasses always inherit all the features of the parent class?
Yes, for all practical purposes. Some object-oriented programming languages allow inherited features to be suppressed, but this is generally considered to be bad practice.
6 How can I recognize an abstract class?
Technically, an abstract class is one that has no m e t h o d defined for one or more of its operations; this means that there can be no instances of the class in a system.
1 10 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
Chapter summary
Software produced using a structured development approach still has many problems particularly relating to maintenance, reuse and testing. These failings are perceived to be due to the lack of a suitable software construct. The object-oriented approach is based on the object, a software construct which should overcome the problems suffered by structured software.
Objects are based on the data in a system, but are also able to provide the required functionality. Every object belongs to a class, which determines its attributes, behaviour and relationships. A good class should demonstrate the qualities listed in the chapter. It should produce objects that are autonomous, cohesive and easy to understand. We have discussed how objects encapsulate data, together with operations to manipulate it, in a single construct, and hide the data behind a public interface of operations. We have also seen that abstract classes can be used to build some future proofing into the system.
There are three main ways in which classes can be linked:
association, aggregation and inheritance. Inheritance is a powerful technique that allows us to create new classes by specializing existing ones; it is therefore an important tool in reuse of software.
Inheritance also allows polymorphism, where an operation can be implemented in different ways by different classes.
Bibliography
Bennett, S., McRobb, S. and Farmer, R. (2002)
Object-Oriented Systems Analysis and Design Using UML
(2nd edition), McGraw- Hill, London.Britton, C. and Doake, J. (2000)
Object-Oriented Development: A Gentle Introduction,
McGraw-Hill, London.Systems
Brown, D. (1997)
Object-Oriented Analysis: objects in plain English,
John Wiley, New York.Fowler, M. (2ooo)
UML Distilled: A Brief Guide to the Standard Object Modeling Language
(2nd edition), Addison-Wesley, Reading, MA.Howe, D. (2ool)
Data Analysis for Database Design
(3rd edition), Butterworth-Heinemann, Oxford.Larman, C. (1998)
Applying UML and Patterns: An Introduction to
Object-Oriented Analysis and Design,
Prentice Hall, New Jersey.(~BJECTS AND CLASSES: THE BASIC CONCEPTS QUICK CHECK QUESTIONS
111
Quatrani, T. (1998)
Visual Modeling with Rational Rose and UML,
Addison-Wesley, Reading, MA.Rumbaugh, J., Blaha, M., Premerlani, W., Eddy, F. and Lorensen, W. (1991)
Object-Oriented Modeling and Design,
Prentice-Hall, Englewood Cliffs, NJ.Stevens, P., with Pooley, R. (2000)
Using UML. Software Engineering with Objects and Components
(updated edition), Addison-Wesley, Harlow.Quick check questions
You can find the answers to these in the chapter.
a Developing systems using a structured approach can result in problematic software. List three problems associated with the structured approach.
b List four qualities that are desirable in a software construct.
c W h a t is meant by the term
seamless development?
d W h a t is the difference between a class and an object?
e W h a t is the difference in the UML diagrammatic notation for a class and an object?
f W h a t do we mean when we refer to the behaviour of an object?
g How does the state of an object affect its behaviour?
h W h a t is meant by the term
encapsulation?
i How do objects communicate?
j W h a t do we mean when we talk about the public interface of an object?
k W h a t is instantiation?
1 List three types of relationships between classes. Briefly describe each.
m W h y might you decide to model one class as a subclass of another?
n W h a t is an abstract class? W h y do we use them?
o W h a t is the difference between an operation and a method?
W h a t is the significance of this for polymorphism?
p W h a t is dynamic binding?
q W h a t is the difference between aggregation and composition?
1 12
A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENTExercises
4.1 This is an exercise to do in tutorial groups. Each group should be supplied with a large piece of paper such as one from a flip chart and a couple of marker pens.
a Get into groups of three or four students.
b Place the contents of your pockets and bags on the desk in front of you.
Arrange the items in categories such as stationery (pens, notebooks, etc.), communication (mobile phones, etc.), garments (hats, gloves, etc.).
d Study the categories and try to refine them into subcate- gories, e.g. within the stationery group there might be writing implements, electronic devices, etc.
e Repeat d) until you can refine no longer.
See if any of the items on the desk can be taken apart (without breaking them). For example, a pen can have its top taken off, a pair of gloves can be split into the right- hand glove and the left-hand glove.
g On the large piece of paper or a board draw a diagram to represent your categories such as the one in Figure 4.35.
The categories are arranged in a hierarchy with the top rectangle representing the original group of items, the next down your initial categorization, etc. Draw a line