0% found this document useful (0 votes)
9 views

Imt304 Object Oriented Programming

Programming

Uploaded by

fareedahmusa5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Imt304 Object Oriented Programming

Programming

Uploaded by

fareedahmusa5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IMT304 OBJECT ORIENTED PROGRAMMING

INTRODUCTION

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to
create models of real-world entities. It's based on the concept of encapsulation, inheritance, and
polymorphism.

A programming paradigm is a fundamental style or approach to writing software code, based on a


set of principles, concepts, and methods that shape the way developers design, organize, and write
programs. It's a way of thinking, a mindset, and a set of best practices that guide the development
process.

Common programming paradigms include:

- Object-Oriented Programming (OOP)

- Functional Programming (FP)

- procedural Programming (PP)

- Logic Programming (LP)

Definition of OOP

OOP is a way of designing and organizing code that simulates real-world objects and systems. It's
a fundamental shift from traditional procedural programming, which focuses on procedures and
functions.

History of OOP

The concept of OOP dates back to the 1960s, but it gained popularity in the 1980s with the
development of languages like Smalltalk and C++. Today, OOP is the foundation of modern
programming languages like Java, C#, and Python.

BASICS OF OBJECT-ORIENTED PROGRAMMING

Here are the basics of Object-Oriented Programming (OOP)

1. Classes: A class is like a blueprint or a plan for creating objects.

Here's an example using a program's components:

Parent Class (Superclass/global): `Vehicle`

Attributes: `color`, `model`, `year`

1
- Behaviour/Methods: `startEngine()`, `accelerate()`, `brake()`

Child Class (Subclass): `Car`

- Inherits attributes and methods from `Vehicle`

- Additional attributes: `numDoors`, `transmissionType`

- Additional methods: `openTrunk()`, `lockDoors()`

In this example:

- `Car` is the child class that inherits the attributes and methods of the parent class `Vehicle`.

- `Car` also has its own unique attributes and methods that are not part of the `Vehicle` class.

By inheriting from `Vehicle`, `Car` can reuse the common attributes and methods, and then add
its own specific features. This reduces code duplication and makes the program more organized
and efficient.

Other child classes, like `Truck` or `Motorcycle`, could also inherit from `Vehicle` and add their
own unique features, creating a hierarchy of classes that share common characteristics and
behaviors.

2. Objects: An object is something that has its own characteristics (attributes) and actions

In programming, an object is an instance of a class, which represents a real-world entity or concept.


Think of it like a physical object, like a book or a chair.

Here's an example:

Class: `Book`

- Attributes: `title`, `author`, `pages`

- Methods: `open()`, `read()`, `close()`

Object: `myBook`

- Instance of the `Book` class

- Has its own set of attributes:

- `title`: "The Great Gatsby"

- `author`: "F. Scott Fitzgerald"

- `pages`: 180

2
- Can use the methods defined in the `Book` class:

- `myBook.open()`

- `myBook.read()`

- `myBook.close()`

In this example:

- `myBook` is an object, an instance of the `Book` class.

- It has its own set of attributes, which are specific to this particular book.

- It can use the methods defined in the `Book` class, which are common to all books.

Objects have three main characteristics:

1. State: The object's attributes, which describe its current situation.)

2. Behavior: The object's methods, which define its actions and interactions.

3. Identity: The object's unique existence, which distinguishes it from other objects.

By creating objects, programmers can model real-world entities and systems, making it easier to
write efficient,

organized, and maintainable code. (methods).

3. Attributes: Attributes are the characteristics of an object, like its name, age, or color.

In programming, an attribute is a characteristic or property of an object that defines its state or


status. Think of it like a label or a tag that describes a specific aspect of the object.

Here's an example:

Object: `myCar`

- Attributes:

- `color`: "red"

- `model`: "Toyota"

- `year`: 2015

In this example:

- `color`, `model`, and `year` are attributes of the `myCar` object.

3
- Each attribute has a value:

- `color` has the value "red"

- `model` has the value "Toyota"

- `year` has the value 2015

Attributes are used to:

- Describe the object's characteristics

- Store data about the object

- Define the object's state or status

Think of attributes like adjectives that describe a noun (the object). Just as adjectives describe the
characteristics of a person or thing, attributes describe the characteristics of an object in a program.

Some common examples of attributes include:

- Physical characteristics: `height`, `weight`, `color`

- Identifying information: `name`, `ID`, `serialNumber`

- Status or state: `isOpen`, `isRunning`, `isAvailable`

By using attributes, programmers can create objects that have unique characteristics and behaviors,
making it easier to write efficient and effective code.

4. Behaviour/Methods: Methods are the actions that an object can perform, like walking, talking,
or jumping.

In programming, a behavior or method is an action that an object can perform or a task that it can
execute. Think of it like a verb that describes what the object can do.

Here's an example:

Object: `myCar`

- Attributes:

- `color`: "red"

- `model`: "Toyota"

- `year`: 2015

- Behaviors/Methods:

4
- `startEngine()`: starts the car's engine

- `accelerate()`: increases the car's speed

- `brake()`: slows down or stops the car

In this example:

- `startEngine()`, `accelerate()`, and `brake()` are behaviors or methods of the `myCar` object.

- Each behavior/method performs a specific action:

- `startEngine()` starts the car's engine

- `accelerate()` increases the car's speed

- `brake()` slows down or stops the car

Behaviors/Methods are used to:

- Define what actions an object can perform

- Describe how an object interacts with other objects or its environment

- Encapsulate complex logic or calculations

Think of behaviors/methods like verbs that describe what an object can do. Just as verbs describe
actions in the physical world, behaviors/methods describe actions in the programming world.

Some commo n examples of behaviors/methods include:

- Actions: `run()`, `jump()`, `read()`

- Calculations: `calculateTotal()`, `generateReport()`, `encryptData()`

- Interactions: `sendMessage()`, `makePhoneCall()`, `sendEmail()`

5. Inheritance: Inheritance is when one class copies the characteristics and actions of another
class.

Inheritance is like a family relationship in programming. A child class inherits the attributes and
behaviors of a parent class, just like how a child inherits traits and characteristics from their
parents.

Here's an example:

Parent Class: `Vehicle`

- Attributes: `color`, `model`, `year`

5
- Behaviors/Methods: `startEngine()`, `accelerate()`, `brake()`

Child Class: `Car`

- Inherits attributes and behaviors from `Vehicle`

- Additional attributes: `numDoors`, `transmissionType`

- Additional behaviors/methods: `openTrunk()`, `lockDoors()`

In this example:

- `Car` is the child class that inherits the attributes and behaviors of the parent class `Vehicle`.

- `Car` also has its own unique attributes and behaviors that are not part of the `Vehicle` class.

Inheritance allows for:

- Code reuse: `Car` can use the attributes and behaviors of `Vehicle` without duplicating code.

- Hierarchical relationships: `Car` is a specific type of `Vehicle`, and inheritance reflects this
relationship.

- Extension of functionality: `Car` can add new attributes and behaviors that are specific to cars.

Think of inheritance like a family tree:

- `Vehicle` is the parent

- `Car` is the child

- `Car` inherits traits and characteristics from `Vehicle` and adds its own unique features.

This helps programmers create a more organized, efficient, and maintainable codebase.

6. Polymorphism: Polymorphism is when an object can change its behavior depending on the
situation.

Polymorphism is like a shape-shifter in programming. An object can take on multiple forms,


depending on the situation, just like how a shape-shifter can change its shape to adapt to different
environments.

Here's an example:

Object: `myCar`

- Has a method: `move()`

- Can take on multiple forms:

6
- On land: `move()` means driving on wheels

- On water: `move()` means sailing with propellers

- In air: `move()` means flying with wings

In this example:

- `myCar` is the object that can take on multiple forms.

- The `move()` method is polymorphic, meaning it can behave differently depending on the
situation.

Polymorphism allows for:

- Flexibility: objects can adapt to different situations

- Generic code: write code that works with multiple types of objects

- Easier maintenance: change behavior without changing code

Think of polymorphism like a shape-shifter:

- `myCar` is the shape-shifter

- `move()` is the shape-shifting ability

- Different situations are like different environments, and `myCar` adapts its behavior to fit.

This helps programmers write more flexible, reusable, and efficient code.

7. Encapsulation: Encapsulation is when an object hides its internal details and only shows what's
necessary.

Encapsulation is like a protective bubble in programming. An object hides its internal details and

only shows what's necessary, like a bubble that protects its contents from the outside world.

Here's an example:

Object: `myBankAccount`

- Has internal details: `accountBalance`, `accountNumber`

- Has a public interface: `deposit()`, `withdraw()`, `checkBalance()`

- The internal details are hidden, like inside a bubble

- The public interface is like the surface of the bubble, allowing controlled access

7
In this example:

- `myBankAccount` is the object with the protective bubble

- The internal details (`accountBalance`, `accountNumber`) are hidden from the outside world

- The public interface (`deposit()`, `withdraw()`, `checkBalance()`) allows controlled access to the
object's functionality

Encapsulation helps:

- Hide sensitive data: protect internal details from unauthorized access

- Improve code organization: keep internal details private and public interface separate

- Reduce complexity: only expose necessary information to the outside world

Think of encapsulation like a protective bubble:

- Object is the contents inside the bubble

- Internal details are the sensitive information inside

- Public interface is the surface of the bubble, controlling access.

8. Abstraction: Abstraction is when an object shows only what's important and hides the rest.

Benefits of OOP

OOP offers several benefits, including:

Modularity: OOP allows you to break down complex systems into smaller, independent modules.

Reusability: OOP enables code reusability, which saves time and effort.

Easier Maintenance: OOP makes it easier to modify and maintain code.

Improved Readability: OOP promotes self-documenting code, which makes it easier to understand.

You might also like