Key Concepts in Object-Oriented Analysis

ICT2622 - Object-Oriented Analysis · Introduction to Object-Oriented Analysis

Key Concepts in Object-Oriented Analysis

Object-oriented analysis (OOA) is a method for analysing and designing a system using the principles of object-oriented programming (OOP). OOA focuses on identifying the objects that will be part of the system and how these objects interact with each other. In this section, you will learn about the key concepts that are fundamental to object-oriented analysis.

Objects

In OOA, an object is an instance of a class. It represents a real-world entity or concept. Each object has attributes (data) and methods (functions or procedures). For example, consider a Car object. It may have attributes such as colour, make, and model, and methods such as start() and stop().

Remember: An object is defined by its attributes and methods.

Classes

A class is a blueprint for creating objects. It defines the attributes and methods that the objects created from the class will have. Using the previous example, a Car class can define attributes like colour, make, and model, and methods like start() and stop(). Here is how you might define a simple class in C++:

class Car {
public:
string colour;
string make;
string model;
void start() {
// code to start the car
}
void stop() {
// code to stop the car
}
};

Encapsulation

Encapsulation is a principle that restricts access to the internal state of an object. It ensures that the object's data is hidden from the outside world and can only be accessed through well-defined methods. This helps to protect the integrity of the object's data. In C++, you can achieve encapsulation by using access specifiers such as private, protected, and public.

Tip: Use private for attributes that should not be accessible directly from outside the class.

Here is an example of encapsulation in a class:

class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};

Inheritance

Inheritance allows one class to inherit attributes and methods from another class. This promotes code reusability and establishes a relationship between classes. The class that inherits is called the subclass, and the class it inherits from is called the superclass.

For example, if you have a Vehicle class, you can create a Car class that inherits from it:

class Vehicle {
public:
void start() {
// code to start the vehicle
}
};

class Car : public Vehicle {
public:
void honk() {
// code to honk the car
}
};

Polymorphism

Polymorphism allows methods to do different things based on the object that it is acting upon. It enables one interface to be used for a general class of actions. The two types of polymorphism are compile-time (or static) polymorphism and run-time (or dynamic) polymorphism.

An example of compile-time polymorphism is method overloading, where multiple methods have the same name but different parameters:

class MathOperations {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
};

Run-time polymorphism is achieved through inheritance and virtual functions. Here is an example:

class Animal {
public:
virtual void makeSound() {
// generic animal sound
}
};

class Dog : public Animal {
public:
void makeSound() override {
// dog barks
}
};

Watch out: Remember to use the virtual keyword in the base class to enable run-time polymorphism.

Abstraction

Abstraction is the process of hiding the complex implementation details and showing only the essential features of an object. It simplifies the interface and reduces complexity. In C++, you can achieve abstraction using abstract classes and interfaces.

An abstract class is a class that cannot be instantiated and can contain pure virtual functions. Here is an example:

class Shape {
public:
virtual void draw() = 0; // pure virtual function
};

class Circle : public Shape {
public:
void draw() override {
// code to draw a circle
}
};

Relationships between Objects

Understanding the relationships between objects is crucial in OOA. There are several types of relationships:

  • Association: A relationship where one object uses or interacts with another object. For example, a Driver object can be associated with a Car object.
  • Aggression: A special type of association where one object contains or is made up of other objects. For example, a Car object may have an Engine object as part of it.
  • Inheritance: As discussed earlier, this is a relationship where one class inherits from another.
  • Dependency: A relationship where one object depends on another object. For example, a Car object may depend on a Fuel object to operate.

Use Cases

Use cases are a technique used to identify the functional requirements of a system. They describe the interactions between users (actors) and the system. A use case consists of a name, actors, preconditions, postconditions, and a flow of events.

For example, consider a use case for a banking system:

Use Case: Withdraw Money
Actors: Customer, ATM
Preconditions: Customer has an account and sufficient balance.
Postconditions: Customer's balance is updated.
Flow of Events:
1. Customer inserts card.
2. Customer enters PIN.
3. Customer selects withdrawal amount.
4. ATM dispenses cash.

Class Diagrams

A class diagram is a visual representation of the classes and their relationships in a system. It shows the attributes, methods, and associations between classes. Class diagrams are essential for understanding the structure of the system.

Here is a simple class diagram for the Car and Engine classes:

+--------------------+
| Car |
+--------------------+
| - colour: string |
| - make: string |
| - model: string |
+--------------------+
| + start() |
| + stop() |
+--------------------+
|
|
|
|
+--------------------+
| Engine |
+--------------------+
| - type: string |
+--------------------+
| + start() |
+--------------------+

Conclusion

In this topic, you have learned about the key concepts in object-oriented analysis, including objects, classes, encapsulation, inheritance, polymorphism, abstraction, relationships, use cases, and class diagrams. Understanding these concepts is essential for successfully applying object-oriented principles in your analysis and design of information systems.

Check your understanding

  • What is the difference between a class and an object?
  • Explain the principle of encapsulation and provide an example.
  • What is polymorphism and how is it implemented in C++?
  • Describe the importance of use cases in object-oriented analysis.