What Is ObjectOrientation Based on Chapter 4 of
What Is Object-Orientation? Based on Chapter 4 of Bennett, Mc. Robb and Farmer: Object Oriented Systems Analysis and Design Using UML, (2 nd Edition), Mc. Graw Hill, 2002. 03/12/2001 © Bennett, Mc. Robb and Farmer 2002 1
In This Lecture You Will Learn: The fundamental concepts of objectorientation n The justifications for an object-oriented approach n 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 2
Basic Concepts n The main concepts introduced here are: – Objects, Classes and Instances – Object State – Generalization and Specialization – Message-passing and Encapsulation – Polymorphism 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 3
Objects An object is: “an abstraction of something in a problem domain, reflecting the capabilities of the system to – keep information about it, – interact with it, – or both. ” Coad and Yourdon (1990) 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 4
Objects “Objects have state, behaviour and identity. ” Booch (1994) n State: the condition of an object at any moment, affecting how it can behave n Behaviour: what an object can do, how it can respond to events and stimuli n Identity: each object is unique 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 5
Examples of Objects Object Identity Behaviour State A person. ‘Hussain Pervez. ’ Speak, walk, read. Studying, resting, qualified. A shirt. My favourite button white denim shirt. Shrink, stain, rip. Pressed, dirty, worn. A sale. Sale no #0015, 16/06/02. Earn loyalty points. Invoiced, cancelled. A bottle of ketchup. This bottle of ketchup. Spill in transit. Unsold, opened, empty. 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 6
Message-passing Several objects may collaborate to fulfil each system action n “Record CD sale” could involve: n – A CD stock item object – A sales transaction object – A sales assistant object n These objects communicate by sending each other messages 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 7
Message Passing n n Think of the system as simulation Structured methods separate data from the processes that act on the data Not possible for all processes to be located with all the data needed, but distributed among many objects Encapsulation – Details can be hidden in objects – This gives rise to information hiding: n Programmers do not need to know all the details of an object 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 8
Message-passing and Encapsulation ‘Layers of an onion’ model of an object: Message from another object requests a service. Operation called only via valid operation signature. An outer layer of operation signatures… …gives access to middle layer of operations… …which can access inner core of data 06 January 2002 Data accessed only by object’s own operations. An object’s data is hidden (encapsulated). © Bennett, Mc. Robb and Farmer 2002 9
Comparison of Structural and OO n. Procedural paradigm: – Software is organized around the notion of procedures – Procedural abstraction n Works as long as the data is simple – Adding data abstractions n Groups together the pieces of data that describe some – Such as Records and structures n. Object oriented paradigm: – Organizing procedural abstractions in the context of data abstractions 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 10
A View of the Two paradigms 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 11
Class and Instance All objects are instances of some class n Class: “a description of a set of objects with similar n – attributes, – operations, – methods – relationships and semantics. ” OMG (2001) 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 12
Class and Instance “The purpose of a class is to declare a collection of methods, operations and attributes that fully describe the structure and behaviour of objects. ” OMG (2001) n Structure: what an object knows, information that it holds n Behaviour: what an object can do 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 13
Class and Instance An object is: “an instance that originates from a class, it is structured and behaves according to its class. ” OMG (2001) 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 14
Class membership n Based on logical similarity – Descriptive characteristics – Valid behaviors n Staff member vs Client – Name, staff number, start date – Name, address, telephone number, fax number, e-mail address 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 15
Generalization and Specialization Classification is hierarchic in nature n For example, a person may be an employee, a customer, a supplier of a service n An employee may be paid monthly, weekly or hourly n An hourly paid employee may be a driver, a cleaner, a sales assistant n 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 16
Specialization Hierarchy More general (superclasses) Person Employee monthly paid Customer weekly paid Driver 06 January 2002 Supplier hourly paid Cleaner Sales More specialized assistant (subclasses) © Bennett, Mc. Robb and Farmer 2002 17
Generalization and Specialization n More general bits of description are abstracted out from specialized classes: General (superclass) Person Name Date of birth Gender Title 06 January 2002 Specialized (subclass) Hourly. Paid. Driver Start. Date Standard. Rate Overtime. Rate Licence. Type © Bennett, Mc. Robb and Farmer 2002 18
The Isa Rule n. Always check generalizations to ensure they obey the isa rule –“A checking account is an account” –“A village is a municipality” n. Should ‘Province’ be a subclass of ‘Country’? –No, it violates the isa rule n “A province is a country” is invalid! 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 19
Inheritance n The whole description of a superclass applies to all its subclasses, including: – Information structure – Behaviour Often known loosely as inheritance n (Actually, inheritance is the facility in an O-O language that implements generalization / specialization) n 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 20
Disjoint nature n Branches diverge further from the root of the tree – Not allowed to converge – Multiple inheritance n Generalization structures are abstraction that we choose to apply, not necessarily equals to real world 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 21
Polymorphism allows one message to be sent to objects of different classes n Sending object need not know what kind of object will receive the message n Each receiving object knows how to respond appropriately n For example, a ‘resize’ operation in a graphics package n 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 22
Polymorphism in Resize Operations <<entity>> Campaign title campaign. Start. Date campaign. Finish. Date get. Campaign. Adverts() add. New. Advert() 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 23
Advantages of O-O n Can save effort – Reuse of generalized components cuts work, cost and time n Can improve software quality – Encapsulation increases modularity – Sub-systems less coupled to each other – Better translations between analysis and design models and working code 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 24
Difficulties and Risks in OOP n. Language evolution and deprecated features: – Java can be less efficient than other languages n n VM-based Dynamic binding n. Efficiency can be a concern in some object oriented systems – Java is evolving, so some features are ‘deprecated’ at every release – But the same thing is true of most other languages 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 25
Summary In this lecture you have learned about: n The fundamental concepts of O-O – Object, class, instance – Generalization and specialization – Message-passing and polymorphism n Some of the advantages and justifications of O-O 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 26
References Coad and Yourdon (1990) n Booch (1994) n OMG (2001) n (For full bibliographic details, see Bennett, Mc. Robb and Farmer) 06 January 2002 © Bennett, Mc. Robb and Farmer 2002 27
- Slides: 27