260 likes | 271 Views
Learn about the basic concepts of objects and classes in object-oriented systems, including use cases, attributes, behavior, state, identity, and encapsulation. Understand the importance of identifying objects and classes and their relationships in a system.
E N D
A Student Guide to Object- Orientated Systems Chapter 4 Objects and Classes: the basic concepts
Objects and Classes – the basic concepts • Use cases model the user’s view of the functionality of a system. Each use case represents a task or major chunk of functionality • Object orientated software structure based on objects • Objects must deliver all the system functionality
What is an object? The most important concept in object-orientated development • A representation of something in the application domain about which we need to store data to enable the system to provide the required functionality.
aBike :Bike type = men’s dailyHireRate = £8 deposit = £50 What is an object? 2 sections, top is the object’s name, bottom is the object’s attribute values Object name – two optional parts, both underlined aBike = this specific object’s name Bike = the class the object belongs to • The object’s attribute values • Attributes are data items defined as part of an object or class. • type is an attribute • men’s is the value of the ‘type’ • attribute for this object
More about objects • Every object belongs to a class • Objects in an O-O system can represent • physical things (customers, products, members, and books), • conceptual things (orders, loans, reservations and cancellations) • organizational things (companies or departments). • computer implementation features (GUI windows, files or linked lists)
More about objects • Every object in a system has three characteristics • Behaviour, real world objects have certain attributes and behaviour • State, object behaviour may vary depending on its state • Identity, each object has a unique identity and existence
Object characteristics – behaviour Real world objects • Car has data and characteristic behaviour. Data – amount of fuel, engine temperature, speed. Behaviour – stop, start, move. Object orientated objects • In OO data becomes attributes • Behaviour becomes operations (procedures) • Data and behaviour are packaged together
Object characteristics – state An object’s state is determined by the value of its attributes e.g. Bank a/c with no overdraft allowed Attribute determining state = balance Value of balance attribute = £100 Behaviour – Withdrawal of £90 allowed Withdrawal of £150 denied Value of balance attribute = £300 Behaviour –
Object characteristics – identity • Every object has an identity • Every object is a unique • Each object has a separate existence and ultimately a separate space in memory • Objects whose attribute values are identical are totally distinct from one another.
Identification Of Objects • Many methodologists have their own favorite approaches • All techniques have short comings • fail to identify all objects • identifies false objects
Using The Things To Be Modeled • Basic Steps Are: • Identify individual or group things, such as persons, roles, organizations, logs, reports, forms, etc. in the application domain • Identify the corresponding objects and classes • Requires significant experience with OO to apply successfully
Using Nouns • This assumes that written documents about the domain exist • Steps are: • Obtain or author a narrative English text that represents an informal description of the problem to be solved. • Use the nouns, pronouns, and noun phrases to identify objects and classes • Verbs and predicate phrases are used to identify services
Encapsulation • Encapsulation - packaging related data and operations together • Data hiding – making the internal details of an object inaccessible to another object • Public interface – provides the services an object makes available to other objects.
Encapsulation Data inside an object is surrounded by a protective ring of operations The data is protected by the operations that encapsulate it. Operations data Public interface – an object’s name, class and operations, the only parts of the object accessible to other objects.
Class • An object is defined in terms of its class • A class of objects is a group of objects with the same set of attributes, relationships and behaviour • An object is an instance of a class • Instantiation is creating a new object
bike# available type size make model dailyHireRate deposit getCharges (no.days) findBike (bike#) registerBike (bikeDetails) getBike# () Class Class name – starts with capital letter, if two words ‘CarPark’ Bike Attributes – lower case, no spaces, each subsequent word starts with capital letter Operations – same notation as attributes
Relationships Three types of relationships • Association • Aggregation • Generalization (Inheritance) • Dependency • These are links between classes that are used for message passing • A navigable path between objects
Association – a link between two classes indicating a relationship studies Student Course 0..* 0..* 1..* 1..* • Association relationship between Student and Course classes • The association can be named: a student studies a course. • An association has two ends each attached to a class. • The numbers and asterisk on the line indicate the multiplicity of the association.
Aggregation – a whole-part relationship Identified by - 'consists of', 'has a', or 'is a part of‘ Wheels, doors and engine are ‘part’ of a car
Composition – tighter aggregation • the whole object has exclusive ownership of its parts i.e. the part object can only participate in one aggregation; • the parts live and die with the whole
StaffCard cardNumber name dept expiryDate delete () VisitorsCard cardNumber currentDate delete () Inheritance and generalization Inheritance – mechanism for defining a new class in terms of an existing class Generalization – moving common attributes and operations in to a more general class
Inheritance and generalization • Relationship described as • ‘is-a’ • ‘is–a-type-of’ • ‘is–a–kind–of’ • a specialized class inherits from a general class • a subclass inherits from a superclass • a child class inherits from a parent class • a derived class inherits from a base class.
Card {abstract} cardNumber delete() Abstract class – never instantiated Inheritance a relationship between classes Card objects will never be created (instantiated)
Polymorphism • The term polymorphism means the ability for a single message to produce a different response depending on the class of the object to which it is sent. • Polymorphism linked with an inheritance hierarchy allows a single message to be interpreted differently by different objects. • Which method is executed will depend on which object receives the message. • A single operation may be implemented by more than one method.