Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Week 1: Introduction to Object-Oriented Analysis and Design

 Introduce the basics of object-oriented programming (OOP) and Object-Oriented Analysis


Design (OOAD)
 Define objects, classes, and instances
 Explain the characteristics of an object: state, behavior, and identity
 Explain inheritance, polymorphism, and encapsulation
 Use simple examples to illustrate these concepts (e.g. a car object with attributes like color and
speed, and methods like accelerate and brake)

 More simple examples:


 A Car object with attributes (color, speed) and methods (accelerate, brake)
 A BankAccount class with inheritance (CheckingAccount, SavingsAccount)
 A Student class with encapsulation (private attributes, public methods)
 Examples of objects: a car, a student, a bank account
 Examples of classes: Vehicle, Person, BankAccount
 Examples of inheritance: a Car is a type of Vehicle, a Student is a type of Person
 Examples of polymorphism: a Vehicle can be a Car or a Motorcycle, a Person can be a Student
or a Teacher
 Examples of encapsulation: a BankAccount has a balance and methods to deposit and withdraw
money

Introduction to the basics of object-oriented programming


(OOP)
Object-oriented programming (OOP) is a programming paradigm that revolves around the
concept of "objects." These objects are instances of classes, which are templates for creating
objects. OOP focuses on organizing code into reusable and modular components, making it
easier to manage and maintain large-scale software projects.

Here are some of the key concepts in OOP:

1. Classes and Objects: A class is a blueprint for creating objects. It defines the properties
(attributes) and behaviors (methods) that all objects of that class will have. An object is
an instance of a class.
2. Encapsulation: Encapsulation is the bundling of data (attributes) and methods that
operate on the data into a single unit, called a class. It hides the internal state of an object
and only exposes the necessary functionality through methods.
3. Inheritance: Inheritance is a mechanism where a new class inherits properties and
behaviors from an existing class. The existing class is called the superclass or parent
class, and the new class is called the subclass or child class. Subclasses can extend or
override the functionality of the superclass.
4. Polymorphism: Polymorphism allows objects of different classes to be treated as objects
of a common superclass. It enables code to work with objects of multiple types and
classes through a single interface. Polymorphism can be achieved through method
overriding and method overloading.
5. Abstraction: Abstraction is the process of hiding the implementation details of a class
and showing only the essential features of the object. It helps in reducing complexity and
managing code by focusing on what an object does rather than how it does it.
6. Association, Aggregation, and Composition: These are different forms of relationships
between classes. The association represents a relationship where objects of one class are
connected to objects of another class. Aggregation represents a "has-a" relationship,
where one class contains references to other classes as part of its state. Composition is a
stronger form of aggregation, where one class is composed of one or more other classes
and has ownership of those objects.

OOP promotes code reusability, modularity, and extensibility, making it a popular choice for
developing large and complex software systems. By organizing code into classes and objects,
OOP enables developers to better manage software complexity and build more maintainable and
scalable applications.

Introduction to the basics of Object-Oriented Analysis and


Design (OOP)
Object-Oriented Analysis and Design (OOAD) is a methodology for analyzing, designing, and
implementing software systems based on the principles of object-oriented programming (OOP).
It focuses on understanding the problem domain, identifying objects and their interactions, and
designing a solution using object-oriented concepts.

Here are the key phases and concepts in Object-Oriented Analysis and Design:

1. Requirements Gathering: This phase involves gathering and understanding the


requirements of the system from stakeholders. It aims to identify the goals of the system,
its functionalities, constraints, and user expectations.
2. Analysis: In this phase, the requirements gathered are analyzed to identify the objects in
the problem domain, their attributes, behaviors, and relationships. Use case diagrams,
class diagrams, and sequence diagrams are commonly used to model the system's
structure and behavior during analysis.
3. Design: The design phase translates the analysis models into implementation-ready
specifications. It involves designing the architecture of the system, defining classes, their
interfaces, relationships, and interactions. Design patterns, such as Singleton, Factory,
and Observer, are often used to address common design problems and improve the
system's flexibility and maintainability.
4. Implementation: In this phase, the designs are implemented using a programming
language. Object-oriented languages like Java, C++, and Python are commonly used for
implementing OOAD designs. Developers write code for classes, methods, and other
components defined during the design phase.
5. Testing: Testing is an integral part of the OOAD process to ensure that the system meets
its requirements and functions correctly. Different testing techniques, such as unit testing,
integration testing, and system testing, are employed to validate the system's behavior
and performance.
6. Maintenance: Once the system is deployed, it enters the maintenance phase, where
updates, enhancements, and bug fixes are made as necessary. OOAD emphasizes
designing for change, making it easier to maintain and evolve the system over time.

Key principles and practices in OOAD include:

 Modularity: Breaking down the system into smaller, cohesive modules (classes) that
encapsulate related functionality.
 Abstraction: Hiding the implementation details of a class and exposing only the essential
features.
 Encapsulation: Bundling data (attributes) and methods that operate on the data into a
single unit (class).
 Inheritance: Creating new classes (subclasses) based on existing classes (superclasses)
to reuse and extend functionality.
 Polymorphism: Allowing objects of different classes to be treated as objects of a
common superclass, enabling code reuse and flexibility.

By following OOAD principles and practices, developers can create well-structured,


maintainable, and scalable software systems that align closely with the requirements of the
stakeholders.

Definition of objects, classes, and instances in OOP


1. Objects: In object-oriented programming (OOP), an object is a tangible entity that
represents a real-world entity, concept, or thing. It is an instance of a class and possesses
state, behavior, and identity. The state of an object is represented by its attributes or
properties, and its behavior is defined by the methods or functions associated with it. For
example, in a banking application, an object representing a bank account might have
attributes such as account number, balance, and owner name, and behaviors such as
deposit, withdraw, and check balance.
2. Classes: A class is a blueprint or template for creating objects. It defines the common
structure and behavior that objects of the same type will have. A class encapsulates data
(attributes) and operations (methods) that operate on that data. Think of a class as a
cookie cutter and objects as the cookies cut out from that cutter. For instance, a "Car"
class might have attributes such as make, model, and color, and methods such as start,
stop, and accelerate.
3. Instances: An instance, also known as an object instance, is a specific realization of a
class. It is created based on the blueprint provided by the class. Each instance of a class
has its own set of attributes and can execute the methods defined in the class. In simpler
terms, if a class is a blueprint for a house, an instance would be an actual house built
based on that blueprint. So, if we have a "Car" class, each actual car on the road is an
instance of that class.

In summary, objects are the concrete entities representing things in the real world, classes are the
blueprints or templates defining the structure and behavior of those objects, and instances are
specific realizations of classes, created based on those blueprints.

Definition of objects, classes, and instances in OOAD


In Object-Oriented Analysis and Design (OOAD), the definitions of objects, classes, and
instances remain quite similar to those in general OOP. However, in the context of OOAD, these
concepts are often approached from a more abstract and design-oriented perspective:

1. Objects: In OOAD, objects are the fundamental building blocks that represent entities,
concepts, or things in the problem domain. They encapsulate both data (attributes) and
behavior (methods) related to the entity they represent. Objects are identified during the
analysis phase, where the focus is on understanding the problem domain and its entities.
Each object has a specific state and behavior defined by its class.
2. Classes: Classes in OOAD serve as blueprints or templates for creating objects. They
define the structure and behavior that all instances (objects) of that class will share.
Classes encapsulate attributes and methods that characterize the objects they represent.
During the design phase, classes are identified based on the analysis of the problem
domain. They represent the concepts and relationships discovered during analysis and
provide a way to organize and model the system's components.
3. Instances: In OOAD, instances are specific realizations of classes. Each instance
represents a unique occurrence of an object, with its own state and behavior. Instances are
created based on the class definitions established during the design phase. They inherit
the attributes and behaviors defined by their class and can interact with other instances
through messages and method calls. Instances play a crucial role in modeling the runtime
behavior of the system and are instantiated during the implementation phase.

In OOAD, the focus is on understanding the problem domain, designing a solution using object-
oriented principles, and creating a well-structured and maintainable system. Objects, classes, and
instances are essential concepts that facilitate the modeling and design of complex systems by
providing a way to represent and organize the system's components and behaviors.
Fundamental Characteristics
The characteristics of an object play a crucial role in modeling and designing software systems.
Let's delve into these characteristics within the context of OOAD:

1. State: In OOAD, the state of an object refers to the collection of attributes or properties
that define its current condition or configuration. These attributes encapsulate the data
associated with the object. During the analysis phase of OOAD, identifying the state
involves understanding the essential properties that characterize each object in the
problem domain. For instance, in an e-commerce system, the state of a "Product" object
might include attributes such as name, price, quantity in stock, and description.
2. Behavior: Behavior in OOAD signifies the actions or operations that an object can
perform. These actions are represented by methods or functions associated with the
object, defining its external interface. Behavior encapsulates the functionality that allows
the object to interact with its state and with other objects in the system. During the design
phase of OOAD, determining the behavior involves specifying the operations that the
object can execute to manipulate its state or fulfill its responsibilities. For example, in a
"Product" object, behaviors might include methods such as "updatePrice," "addToCart,"
or "checkAvailability."
3. Identity: Identity in OOAD represents the unique identifier or reference that
distinguishes one object from another within the system. Each object has a distinct
identity, which remains consistent throughout its existence. Identity ensures that objects
can be uniquely identified and referenced within the system, even if they possess the
same state and behavior. In OOAD, identity may be represented by attributes that
uniquely identify an object, such as a customer ID or a product code. Alternatively, a
system-generated identifier, such as a universally unique identifier (UUID), may serve as
an object's identity.

By understanding and modeling the characteristics of objects – state, behavior, and identity –
OOAD enables developers to create comprehensive and effective representations of real-world
entities within software systems. This approach fosters modularity, reusability, and
maintainability, leading to the development of robust and adaptable software solutions.

The characteristics of an object – state, behavior, and identity – work together to define its role
and functionality within a software system. By encapsulating state and behavior, objects provide
a modular and reusable way to represent and manipulate entities in code, while identity ensures
that each object is uniquely identifiable and distinguishable.

Explain inheritance, polymorphism, and encapsulation


In each example, I've used C++ syntax to illustrate the concepts of inheritance, polymorphism,
and encapsulation. These examples should provide a clear understanding of how these principles
work in C++, a widely used object-oriented programming language.
I've also used Java syntax to illustrate the concepts of inheritance, polymorphism, and
encapsulation. These examples should provide a clear understanding of how these principles
work in Java, a widely-used object-oriented programming language.

These are three key concepts in object-oriented programming (OOP), each serving a distinct
purpose in creating robust and maintainable code:

1. Inheritance:

Inheritance is a mechanism in OOP that allows a new class (called a subclass or derived class) to
inherit attributes and behaviors from an existing class (called a superclass or base class). This
promotes code reuse and enables the creation of hierarchical relationships between classes. The
subclass automatically gains access to all public and protected members (attributes and methods)
of the superclass, allowing it to extend or modify its behavior while inheriting its common
features. For example, consider a superclass "Animal" with subclasses like "Dog" and "Cat." The
"Dog" and "Cat" classes inherit common attributes and behaviors from the "Animal" class, such
as "eat" and "sleep," while also having their unique features like "bark" for dogs and "meow" for
cats.

Let's illustrate each of the concepts - inheritance, polymorphism, and encapsulation - with a
simple example using a car object:

class Vehicle {

String color;

public Vehicle(String color) {

this.color = color;

public void move() {

System.out.println("Vehicle is moving");

}
class Car extends Vehicle {

int speed;

public Car(String color, int speed) {

super(color);

this.speed = speed;

public void accelerate() {

System.out.println("Car is accelerating");

public void brake() {

System.out.println("Car is braking");

In Java, inheritance is achieved using the extends keyword. In the Car class definition, you see
class Car extends Vehicle. This means that the Car class inherits all the properties and
methods from the Vehicle class.

Inside the Car class constructor, public Car(String color, int speed), we use the
super(color) statement to call the constructor of the superclass (Vehicle) and initialize its
attributes. This ensures that when a Car object is created, the color attribute from the Vehicle
class is properly initialized.

The Car class also defines additional attributes and methods specific to cars, such as speed,
accelerate(), and brake(). These are unique to the Car class and are not present in the
Vehicle class.
#include <iostream>

using namespace std;

class Vehicle {

public:

string color;

Vehicle(string color) {

this->color = color;

void move() {

cout << "Vehicle is moving" << endl;

};

class Car : public Vehicle {

public:

int speed;

Car(string color, int speed) : Vehicle(color) {

this->speed = speed;

}
void accelerate() {

cout << "Car is accelerating" << endl;

void brake() {

cout << "Car is braking" << endl;

};

In the inheritance example, we have two classes: Vehicle and Car. The Car class inherits from
the Vehicle class using the syntax class Car : public Vehicle. This means that the Car
class inherits all the attributes and methods of the Vehicle class.

Inside the Car class constructor, Car(string color, int speed) : Vehicle(color), we use
the Vehicle(color) syntax to call the constructor of the base class (Vehicle) and initialize its
attributes. This ensures that when a Car object is created, the color attribute from the Vehicle
class is properly initialized.

The Car class also defines additional attributes and methods specific to cars, such as speed,
accelerate(), and brake(). These are unique to the Car class and are not present in the
Vehicle class.

2. Polymorphism:

Polymorphism allows objects of different classes to be treated as objects of a common


superclass, enabling flexibility and extensibility in OOP. It allows a single interface (method or
function) to be used for objects of various types, simplifying code and promoting code reuse.
Polymorphism can be achieved through method overriding and method overloading. Method
overriding occurs when a subclass provides a specific implementation of a method that is already
defined in its superclass. Method overloading involves defining multiple methods with the same
name but different parameter lists within the same class. Polymorphism allows for dynamic
binding, meaning the method to be executed is determined at runtime based on the actual object
type. For example, consider a superclass "Shape" with subclasses like "Circle" and "Rectangle."
A method called "calculateArea()" can be defined in the superclass and overridden in each
subclass to compute the area specific to the shape, such as the area of a circle or rectangle.

class Vehicle {
public void drive() {

System.out.println("Vehicle is being driven");

class Car extends Vehicle {

@Override

public void drive() {

System.out.println("Car is being driven");

In Java, polymorphism is achieved through method overriding. In the Car class, you see
@Override before the drive() method. This annotation indicates that the drive() method is
overriding a method in the superclass (Vehicle).

When you call the drive() method on a Car object, Java looks for the most specific
implementation of the method. Since Car overrides the drive() method, the implementation in
the Car class is executed.

#include <iostream>

using namespace std;

class Vehicle {

public:

virtual void drive() {

cout << "Vehicle is being driven" << endl;

}
};

class Car : public Vehicle {

public:

void drive() override {

cout << "Car is being driven" << endl;

};

In the polymorphism example, we have a base class Vehicle with a virtual method drive().
The drive() method is marked as virtual in the base class, indicating that it can be overridden
by derived classes.

We then have a derived class Car that overrides the drive() method using the override
keyword. This means that when a Car object calls the drive() method, the overridden version in
the Car class will be executed instead of the base class version.

Polymorphism allows us to treat objects of derived classes as objects of the base class, enabling
flexibility and dynamic behavior at runtime.

3. Encapsulation:

Encapsulation is the bundling of data (attributes) and methods (behaviors) that operate on that
data into a single unit, called a class. It hides the internal state of an object and only exposes the
necessary functionality through methods, providing a level of abstraction and ensuring data
integrity. Encapsulation helps in organizing code, reducing complexity, and preventing
unintended access or modification of an object's state from outside the class. It also facilitates
information hiding, allowing changes to the internal implementation without affecting the
external interface. For example, consider a class "BankAccount" with attributes like account
number and balance, and methods like "deposit" and "withdraw." Encapsulation ensures that
these attributes are accessed and modified only through the defined methods, protecting the
integrity of the account data.

class Car {

private String color;


public Car(String color) {

this.color = color;

public String getColor() {

return color;

public void setColor(String color) {

this.color = color;

public void accelerate() {

System.out.println("Car is accelerating");

public void brake() {

System.out.println("Car is braking");

Encapsulation in Java is achieved through access modifiers such as private, protected, and
public. In the Car class, you see private String color;. This makes the color attribute
private to the Car class, meaning it cannot be accessed directly from outside the class.

To interact with the color attribute, public getter and setter methods are provided: public
String getColor() and public void setColor(String color). These methods allow
controlled access to the color attribute, ensuring proper validation and encapsulation of the
internal state of the Car object.

#include <iostream>
using namespace std;

class Car {
private:
string color;

public:
Car(string color) {
this->color = color;
}

string getColor() {
return color;
}

void setColor(string color) {


this->color = color;
}

void accelerate() {
cout << "Car is accelerating" << endl;
}

void brake() {
cout << "Car is braking" << endl;
}
};

In the encapsulation example, we have a Car class with a private attribute color. This means
that the color attribute can only be accessed and modified within the Car class itself; it is not
directly accessible from outside the class.

To interact with the color attribute, we define public member functions getColor() and
setColor(string color). These functions provide controlled access to the color attribute,
allowing users to get and set its value while enforcing any necessary validation logic.

Encapsulation helps in protecting the internal state of objects and promoting data integrity by
controlling access to attributes and providing well-defined interfaces for interaction with objects.
ASSIGNMENTS
Go through the Java code snippets below. Then redo all 10 of them in C++.

Here are 10 simple code snippet assignment questions in Java based on the provided topics,
using a simple student and university connection:

1. Define a Student Class: Write a Java class named "Student" with attributes such as
name, age, and student ID. Include methods to set and get these attributes.

public class Student {


private String name;
private int age;
private int studentID;

// Constructor
public Student(String name, int age, int studentID) {
this.name = name;
this.age = age;
this.studentID = studentID;
}

// Getter methods
public String getName() {
return name;
}

public int getAge() {


return age;
}

public int getStudentID() {


return studentID;
}
}

2. Create Instances of Student: Instantiate two objects of the "Student" class with different
attributes and display their details.

public class Main {


public static void main(String[] args) {
Student student1 = new Student("John", 20, 12345);
Student student2 = new Student("Alice", 22, 67890);

System.out.println("Student 1: Name - " + student1.getName() + ", Age


- " + student1.getAge() + ", ID - " + student1.getStudentID());
System.out.println("Student 2: Name - " + student2.getName() + ", Age
- " + student2.getAge() + ", ID - " + student2.getStudentID());
}
}

3. Characteristics of an Object: Implement a Java class representing a student with


attributes for name, age, and major. Include a method to display the student's details.
public class Student {
private String name;
private int age;
private String major;

// Constructor
public Student(String name, int age, String major) {
this.name = name;
this.age = age;
this.major = major;
}

// Method to display student details


public void displayDetails() {
System.out.println("Name: " + name + ", Age: " + age + ", Major: " +
major);
}
}

4. Inheritance in Java: Define a Java class "UndergraduateStudent" that inherits from the
"Student" class. Add an attribute for year level and a method to display the student's
status.

public class UndergraduateStudent extends Student {


private int yearLevel;

// Constructor
public UndergraduateStudent(String name, int age, String major, int
yearLevel) {
super(name, age, major);
this.yearLevel = yearLevel;
}

// Method to display student status


public void displayStatus() {
System.out.println("Name: " + getName() + ", Year Level: " +
yearLevel);
}
}

5. Polymorphism in Java: Create an interface "Drawable" with a method "draw()" in Java.


Implement this interface in classes "Circle" and "Rectangle" to draw their respective
shapes.

interface Drawable {
void draw();
}

class Circle implements Drawable {


public void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("Drawing a rectangle");
}
}

6. Encapsulation in Java: Implement a Java class "University" with private attributes for
name and location. Include getter and setter methods to access and modify these
attributes.

public class University {


private String name;
private String location;

// Getter methods
public String getName() {
return name;
}

public String getLocation() {


return location;
}

// Setter methods
public void setName(String name) {
this.name = name;
}

public void setLocation(String location) {


this.location = location;
}
}

7. Creating Objects in Java: Write a Java program that creates an object of the "Student"
class and initializes its attributes using constructor parameters. Display the student's
details.

public class Main {


public static void main(String[] args) {
Student student = new Student("Emma", 21, 98765);
System.out.println("Name: " + student.getName() + ", Age: " +
student.getAge() + ", ID: " + student.getStudentID());
}
}

8. Using Inheritance in Java: Define a Java class "GraduateStudent" that inherits from the
"Student" class. Add an attribute for research topic and a method to display the student's
research area.

public class GraduateStudent extends Student {


private String researchTopic;
// Constructor
public GraduateStudent(String name, int age, int studentID, String
researchTopic) {
super(name, age, studentID);
this.researchTopic = researchTopic;
}

// Method to display research area


public void displayResearchArea() {
System.out.println("Research Topic: " + researchTopic);
}
}

9. Applying Encapsulation in Java: Write a Java class "Course" with private attributes for
name and credits. Provide public methods to set and get these attributes safely.

public class Course {


private String name;
private int credits;

// Setter methods
public void setName(String name) {
this.name = name;
}

public void setCredits(int credits) {


this.credits = credits;
}

// Getter methods
public String getName() {
return name;
}

public int getCredits() {


return credits;
}
}

10. Demonstrate Polymorphism in Java: Create an interface "Playable" with a method


"play()" in Java. Implement this interface in classes "Guitar" and "Piano" to play their
respective instruments.

interface Playable {
void play();
}

class Guitar implements Playable {


public void play() {
System.out.println("Playing guitar");
}
}

class Piano implements Playable {


public void play() {
System.out.println("Playing piano");
}
}

These code snippet assignments cover a range of topics related to OOP and OOAD, allowing
students to practice defining classes, creating instances, understanding object characteristics,
implementing inheritance, polymorphism, and encapsulation in Java.

You might also like