0% found this document useful (0 votes)
4 views41 pages

Classes and Objects

The document provides an overview of object-oriented analysis, design, and programming, focusing on the concepts of classes and objects. It defines key characteristics of objects, such as state, behavior, and identity, and discusses their relationships, including aggregation and inheritance. Additionally, it emphasizes the importance of crafting quality abstractions and mechanisms in software development.

Uploaded by

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

Classes and Objects

The document provides an overview of object-oriented analysis, design, and programming, focusing on the concepts of classes and objects. It defines key characteristics of objects, such as state, behavior, and identity, and discusses their relationships, including aggregation and inheritance. Additionally, it emphasizes the importance of crafting quality abstractions and mechanisms in software development.

Uploaded by

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

Object-Oriented Analysis,

Design and Programming:


Classes and Objects

Prof. Eric Amankwa


Introduction and lecture Objectives
• The importance of understanding the materials of a trade
• Comparison: Engineering materials vs. software materials
• Introduction to classes and objects in software
• In this lecture, we will discuss
• The nature of classes, objects, and their relationships, and
• Rules of thumb for crafting quality abstractions and mechanisms.
What are Classes and Objects?
• Classes: Blueprints for creating objects
• Objects: Instances of classes, tangible entities with well-defined
behavior
The Nature of an Object
• An object is defined as an entity that has state, behavior, and identity.
• These three characteristics collectively define the nature of an object in
object-oriented systems.
• State: The state of an object encompasses all the information held by the
object at a given time. It reflects the current values of the object's
attributes or data members.
• Behavior: Behavior refers to how the object acts and reacts in terms of the
messages it receives and the operations it performs. It’s determined by the
methods or functions associated with the object.
• Identity: Identity gives an object a unique existence in the system. Two
objects may have the same state and behavior but are still distinguishable
because they have different identities.
What Is and What Isn’t an Object
An object is:
• A Conceptual or Physical Entity:
Objects can represent tangible entities like a car, book, or person, as
well as intangible ones like a reservation, a transaction, or a process.
What matters is that the entity has a meaningful role in the system
being modeled.
• Something with Identity, State, and Behavior:
• Identity allows an object to be distinguishable from others.
• State captures its current condition through attribute values.
• Behavior defines how it acts or responds via methods.
What Is and What Isn’t an Object
• An object is:
• An Abstraction of Something in the Real or Imagined World:
Objects are abstractions that simplify complex reality. For instance,
the object "Car" may abstract real cars into a model with only the
relevant features like speed, fuel level, and methods like drive() or
stop().
• A Unit of Encapsulation:
An object hides its implementation and only exposes operations
necessary for its interaction. This promotes data integrity and
modularity.
What Isn’t an Object
• Primitive Data Types (by themselves):
Simple data like integers, characters, or floating-point numbers are not
objects because they lack the combination of identity, encapsulated
state, and behavior. However, they can be wrapped into objects (e.g.,
Integer in Java).
• Operations Alone:
Methods or functions without being tied to an object context are not
objects. An object is more than just behavior—it includes state and
identity as well.
• Pure Data Structures (like C-style structs):
A structure that only contains data without behavior or encapsulation is
not an object. Objects bind data with the operations that manipulate that
data.
Identifying Objects
Characteristics of Objects
• Crisp conceptual boundaries
• State, behavior, and identity

Examples of Non-Objects
• Attributes like beauty or color, emotions like love
and anger
Key Properties of Objects
Definition of an Object:
• An entity with state, behavior,
and identity
• The structure and behavior of
similar objects are defined by
a class
State of an Object
• The state of an object is the cumulative values of all its attributes at any point in
time. It reflects the condition or situation of the object and can change over the
object's lifetime.
• The state encompasses both visible attributes (those exposed via public
methods) and hidden attributes (internal/private data).
• The state determines how an object will respond to certain messages (method
calls).
• Objects with the same structure and behavior can be differentiated by their
state.
• Example:
• A BankAccount object might have:
• AccountNumber: 123456
• Balance: GHS 1,000
• This data represents the state of that specific bank account object.
Behavior of an Object
• The behavior of an object is defined by the set of operations or methods it can
perform and the responses it gives when those operations are invoked.
• Behavior is observable through how the object acts and reacts to messages.
• It's encapsulated: users of the object interact with its behavior, not directly with
its internal state.
• Behavior allows an object to enforce constraints and rules, e.g., not allowing a
withdrawal greater than the balance.
• Example:
• For the BankAccount object:
• deposit(amount)
• withdraw(amount)
• getBalance()
• These methods define how the account behaves in response to user or system
interactions.
Encapsulation
• Encapsulation of State
• Objects encapsulate state and behavior
• All state within a system is encapsulated by objects
• Objects are changeable, instantiated, and can be created or
destroyed, whereas simple values are static and unchangeable.
• Importance for capturing abstractions:
• Understanding state is crucial for capturing the full intent of abstractions in
system development.
• Consideration of both state and behavior is necessary for effective object-
oriented design.
Example of an Employee Class
• Consider an abstraction of an
employee record. Figure 3–1 depicts
this abstraction using the Unified
Modeling Language notation for a
class.
• Each part of this abstraction denotes a
particular property of our abstraction
of an employee.
• This abstraction is not an object
because it does not represent a
specific instance.
Example of Two Distinct Objects
• When made specific, we may
have, for example, two distinct
objects: Tom and Kaitlyn, each
of which takes up some amount
of space in memory (see Figure
3–2)
Types of Operations
• An operation denotes a service that a class offers to its
clients.
• The three most common kinds of operations are the
following:
• Modifier: Alters the state of an object.
• Selector: Accesses the state without altering it.
• Iterator: Permits accessing all parts of an object in a defined order.
• Two other kinds of operations are common; they represent
the infrastructure necessary to create and destroy instances
of a class.
• Constructor: Creates and/or initializes an object.
• Destructor: Frees the state of an object and/or destroys the object
Object Roles and Responsibilities
• Object roles and responsibilities as essential elements
in understanding how objects function within a system.
• A role refers to the contextual purpose or function
that an object plays within a particular collaboration or
system.
• An object can assume different roles depending on the
scenario or collaboration in which it is involved.
• Consider an object Person:
• In one context, the person might play the role of a
Student (with responsibilities like submitAssignment()).
• In another context, the same object might play the role
of a Customer (with responsibilities like
makePayment()).
Responsibilities
• Responsibilities are the duties or obligations of an object, usually
defined as:
• Knowing something (knowledge responsibilities)
• Doing something (behavioral responsibilities)
• Responsibilities are assigned to objects during the design phase to
ensure each object contributes meaningfully to system functionality.
• Example, for a BankAccount object:
• Knowing: Its account number, current balance
• Doing: Depositing funds, withdrawing funds, checking balance
Object Identity
• Identity is the property that distinguishes each object from all others, regardless of
their state or behavior.
• Even if two objects have the same state and behavior, they remain distinct entities
because of their identity.
• Identity is often implicit, managed by memory address or object references, not by
attributes visible to the user.
• Identity is critical when objects are stored in collections, passed as parameters, or
compared.
• Example:
• Two BankAccount objects may have:
• Same balance: GHS 1,000
• Same methods
• But they are distinct because:
• One has AccountNumber 123456, the other 654321
• They occupy different memory spaces and have different identities
Relationships among Objects
• Objects collaborate and interact through well-defined relationships to
perform meaningful tasks in a system.
• Understanding these relationships among objects is central to
designing robust, modular, and maintainable object-oriented systems.

• Importance of Collaboration
• Objects work together to contribute to system behavior.
Types of Relationships - Links
• A link is a connection or reference between two objects,
representing an instance of an association.
• It indicates that one object is aware of and can communicate with
another.
• Links are established through references or pointers.
• Example:
• If a Teacher object teaches a Student object, there may be a link from Teacher
to Student.
• A link is a physical or conceptual connection between objects.
Links
• Definition: Specific association through which
one object applies the services of another.

• Example: Flow control system with


FlowController, Valve, and DisplayPanel.

• Roles in Links:
• Controller: Operates on other objects.
• Server: Operated on by other objects.
• Proxy: Can both operate and be operated on by
other objects.
Aggregation ("Has-a" Relationship)
• An aggregation is a whole-part relationship, where one object (the
whole) is composed of other objects (the parts)
• The composite object owns or manages the lifecycle of its parts.
Inheritance defines a generalization-specialization hierarchy
• Aggregation implies a stronger
among classes and objects.relationship than a mere link.
• Example:
• A car object may aggregate Engine, Wheel and Chassis objects.
Aggregation
• Definition: Specialized
association denoting a
whole/part hierarchy.
• Example:
TemperatureController, Heater,
and TemperatureRamp.
• Trade-offs: Encapsulation vs.
loose coupling
Inheritance ("Is-a" Relationship)
• Inheritance defines a relationship in which one class (subclass)
inherits the structure and behavior of another class (superclass).
• It allows for reusability and polymorphism.
• Objects of the subclass can be used wherever the superclass is
expected.
• Example:
• A SavingsAccount class inherits from BankAccount - - -> A SavingsAccount is a
BankAccount
• Inheritance defines a generalization-specialization hierarchy among
classes and objects.
Dependency
• A dependency indicates that one object relies on another to fulfill its
responsibilities, but without ownership or a long-term association.
• This is a looser form of coupling than a link or aggregation.
• Dependencies often show up when an object uses parameters, local
variables, or temporary collaborations.
• Example:
• A ReportGenerator object may temporarily depend on a DatabaseConnection
object to fetch data.

The Nature of a Class:
Understanding Classes and Objects
What is a Class?
• A class is defined as a set of objects that share a common structure,
common behavior, and common semantics.
• It serves as a blueprint for creating instances, or objects, which are
concrete entities existing in time and space.

What Isn't a Class?


• While a class represents a conceptual abstraction, an object is a
tangible instance of a class. Objects that do not share common
structure and behavior cannot be grouped into a class, as they are
unrelated in the context of object-oriented programming.
Interface and Implementation
• The interface of a class provides its external view, defining the
operations that can be performed on instances of the class. This
interface includes:
• Public: Accessible to all clients.
• Protected: Accessible only to the class and its subclasses.
• Private: Accessible only to the class itself.
• Package: Accessible only to classes within the same package.
Relationships Among Classes
• Classes are not isolated; they form relationships that define the
structure of a system. Key relationships include:
• Generalization/Specialization: An "is a" relationship, where a subclass
inherits from a superclass (e.g., a rose is a kind of flower).
• Whole/Part: A "part of" relationship, indicating composition or
aggregation (e.g., a petal is part of a flower).
• Association: A semantic dependency between otherwise unrelated
classes (e.g., ladybugs and flowers).
Inheritance and Polymorphism
Inheritance:
• Inheritance allows a class (subclass) to inherit attributes and methods
from another class (superclass). This creates a hierarchy, where
specialized classes inherit the general structure and behavior from
more general classes. There are two main types of inheritance:
• Single Inheritance: A subclass inherits from one superclass.
• Multiple Inheritance: A subclass inherits from multiple superclasses,
which can introduce complexity such as name collisions and repeated
inheritance.
Single and Multiple Inheritance
Polymorphism
• Polymorphism allows methods to be defined in a way that can
operate on objects of different classes.
• This means that a single function name can invoke different
implementations depending on the class of the object it is called on.
• Polymorphism is key to creating flexible and reusable code.
Aggregation and Composition
• Aggregation defines a whole/part relationship between objects,
which can be either:
• Containment by Value: The part object does not exist independently
of the whole (e.g., a Heater is part of a TemperatureController).
• Containment by Reference: The part object exists independently but
is associated with the whole (e.g., a shareholder and owned stocks).
Distinguishing Inheritance and
Aggregation
• It is crucial to apply the appropriate relationship based on the nature
of the connection between classes.
• If there is an "is a" relationship, inheritance is appropriate.
• If there is a "part of" relationship, aggregation should be used.
Interplay of Classes and Objects
• Definition and Relationship: Classes are blueprints for objects, with
each object being an instance of a class. Classes are typically static,
with fixed existence and semantics, while objects are dynamic,
frequently created and destroyed during application execution.
• Static vs. Dynamic Nature: Classes define the static structure of an
application, such as planes, flight plans, and runways in an air traffic
control system, while instances of these classes, like specific planes
and filed flight plans, are dynamic
Roles in Analysis and Design
• Key Tasks: Developers must identify key abstractions (classes) and
mechanisms (interactions of objects) during analysis and design
phases.
• -Views of Abstractions: Early stages focus on the logical framework
(outside view), while later stages focus on the physical representation
(inside view).
Building Quality Classes and Objects
• Incremental Design: Designing classes and objects is an iterative
process that involves refinement to smooth out initial conceptual flaws.
• How can one know if a given class or object is well designed?
• Design Metrics: Quality of abstractions is measured by coupling,
cohesion, sufficiency, completeness, and primitiveness.
• Coupling: Measures the degree of interdependence between classes.
• Cohesion: Measures the degree of relatedness within a class.
• Sufficiency and Completeness: Ensures that classes capture all necessary
characteristics for meaningful interaction.
• Primitiveness: Focuses on operations that require direct access to the
underlying representation for efficiency.
Choosing Operations and
Relationships
• Crafting Interfaces: Designing class interfaces involves iterative
refinement and balancing between too many and too few methods.
• Operations Criteria: Consider reusability, complexity, applicability, and
implementation knowledge when defining operations.
• Time and Space Semantics: Decide on the performance aspects of
operations, including synchronization in multi-threaded
environments.
• Relationship Selection: Choose appropriate relationships (inheritance,
aggregation, dependency) to optimize class interactions and system
cohesion
Summary
• An object has state, behavior, and identity.
• The structure and behavior of similar objects are defined in their
common class.
• The state of an object encompasses all of the (usually static)
properties of the object plus the current (usually dynamic) values of
each of these properties.
• Behavior is how an object acts and reacts in terms of its state changes
and message passing.
• Identity is the property of an object that distinguishes it from all other
objects.
Summary Cont’d
• A class is a set of objects that share a common structure and a
common behavior.
• The three kinds of relationships include association, inheritance, and
aggregation.
• Key abstractions are the classes and objects that form the vocabulary
of the problem domain.
• A mechanism is a structure whereby a set of objects work together to
provide a behavior that satisfies some requirement of the problem.
• The quality of an abstraction may be measured by its coupling,
cohesion, sufficiency, completeness, and primitiveness.
Questions?
Assignment – Classes and Objects
• Visit www.javatpoint.com for examples on Classes and Object
implementation in Java
• Develop a Java program that involves the use of classes and objects
• Your program should contain a Class with the following:
• Class name = Student
• Properties = ID, Fname, Sname, Level, and ExamScore
• Create five instances (Objects) of the Student Class with the information below:
• Your program should print out data

ID FName Sname Level ExamScore


170986557 Conrad Annor 100 75
179008665 Eric Owusu 100 88
168764334 Audrick Amankwa 200 80
169076543 Samuel Bediako 300 55
170876544 Josephine Ansu 200 90

You might also like