Professional Documents
Culture Documents
OOP Week 1
OOP Week 1
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.
Here are the key phases and concepts in Object-Oriented Analysis and Design:
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.
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.
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.
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;
this.color = color;
System.out.println("Vehicle is moving");
}
class Car extends Vehicle {
int speed;
super(color);
this.speed = speed;
System.out.println("Car is accelerating");
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>
class Vehicle {
public:
string color;
Vehicle(string color) {
this->color = color;
void move() {
};
public:
int speed;
this->speed = speed;
}
void accelerate() {
void brake() {
};
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:
class Vehicle {
public void drive() {
@Override
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>
class Vehicle {
public:
}
};
public:
};
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 {
this.color = color;
return color;
this.color = color;
System.out.println("Car is accelerating");
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 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.
// 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;
}
2. Create Instances of Student: Instantiate two objects of the "Student" class with different
attributes and display their details.
// Constructor
public Student(String name, int age, String major) {
this.name = name;
this.age = age;
this.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.
// Constructor
public UndergraduateStudent(String name, int age, String major, int
yearLevel) {
super(name, age, major);
this.yearLevel = yearLevel;
}
interface Drawable {
void draw();
}
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.
// Getter methods
public String getName() {
return name;
}
// Setter methods
public void setName(String name) {
this.name = name;
}
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.
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.
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.
// Setter methods
public void setName(String name) {
this.name = name;
}
// Getter methods
public String getName() {
return name;
}
interface Playable {
void play();
}
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.