Imt304 Object Oriented Programming
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.
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.
1
- Behaviour/Methods: `startEngine()`, `accelerate()`, `brake()`
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
Here's an example:
Class: `Book`
Object: `myBook`
- `pages`: 180
2
- Can use the methods defined in the `Book` class:
- `myBook.open()`
- `myBook.read()`
- `myBook.close()`
In this example:
- 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.
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,
3. Attributes: Attributes are the characteristics of an object, like its name, age, or color.
Here's an example:
Object: `myCar`
- Attributes:
- `color`: "red"
- `model`: "Toyota"
- `year`: 2015
In this example:
3
- Each attribute has a value:
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.
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
In this example:
- `startEngine()`, `accelerate()`, and `brake()` are behaviors or methods of the `myCar` object.
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.
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:
5
- Behaviors/Methods: `startEngine()`, `accelerate()`, `brake()`
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.
- 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.
- `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.
Here's an example:
Object: `myCar`
6
- On land: `move()` means driving on wheels
In this example:
- The `move()` method is polymorphic, meaning it can behave differently depending on the
situation.
- Generic code: write code that works with multiple types of objects
- 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`
- The public interface is like the surface of the bubble, allowing controlled access
7
In this example:
- 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:
- Improve code organization: keep internal details private and public interface separate
8. Abstraction: Abstraction is when an object shows only what's important and hides the rest.
Benefits of OOP
Modularity: OOP allows you to break down complex systems into smaller, independent modules.
Reusability: OOP enables code reusability, which saves time and effort.
Improved Readability: OOP promotes self-documenting code, which makes it easier to understand.