BIT153 ยท 2078 ยท 10 marks
What is object-oriented programming? How does it differ from procedure-oriented programming? Explain features of object oriented programming.
What is object-oriented programming? How does it differ from procedure-oriented programming? Explain features of object oriented programming.[10]
Quick answer
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects rather than functions and logic. An object is a self-contained unit that bundles together data (attributes) and behavior (methods/functions) that operate on that data.
Object-Oriented Programming: Concepts, Comparison, and Features
1. What is Object-Oriented Programming (OOP)?
In OOP, real-world entities are modeled as objects. For example, a "Car" object has attributes like color, speed, and model, and behaviors like start(), stop(), and accelerate().
Key idea:
"OOP treats data as a critical element and does not allow it to flow freely around the system. It ties data more closely to the functions that operate on it."
2. Procedure-Oriented Programming (POP) vs Object-Oriented Programming (OOP)
Procedure-Oriented Programming (POP)
- Programs are divided into functions/procedures.
- Focus is on what to do (the logic/algorithm).
- Data is shared globally and can be accessed by any function.
- Examples: C, Pascal, FORTRAN.
Comparison Table
| Feature | Procedure-Oriented (POP) | Object-Oriented (OOP) |
|---|---|---|
| Basic Unit | Function / Procedure | Object |
| Focus | Algorithm / Logic | Data |
| Data Access | Data is global, freely accessible | Data is hidden (encapsulated) |
| Approach | Top-down | Bottom-up |
| Data Security | Less secure (no data hiding) | More secure (data hiding) |
| Code Reusability | Limited (functions can be reused) | High (via inheritance) |
| Real-world modeling | Difficult | Natural and easy |
| Maintenance | Harder for large programs | Easier due to modularity |
| Examples | C, Pascal, FORTRAN | C++, Java, Python |
Diagram: Data Flow Comparison
POP: OOP:
Function1 [Object 1]
Function2 <-- Global --> [Object 2] <-- Message Passing -->
Function3 Data [Object 3]
In POP, data flows freely between functions. In OOP, objects communicate through message passing, keeping data protected.
3. Features of Object-Oriented Programming
3.1 Class
- A class is a blueprint or template for creating objects.
- It defines the attributes (data members) and behaviors (member functions) that objects of that class will have.
- Example:
class Student { string name; int roll; void display(); };
3.2 Object
- An object is an instance of a class.
- It is the actual entity created from the class blueprint that occupies memory.
- Example:
Student s1;-- heres1is an object of classStudent.
3.3 Encapsulation
- Encapsulation is the mechanism of wrapping data and functions together into a single unit (class).
- It restricts direct access to some components, protecting the internal state of an object.
- Achieved using access specifiers:
private,protected,public. - Benefit: Prevents accidental modification of data (data hiding).
+-----------------------------+
| Class: BankAccount |
|-----------------------------|
| - balance (private) | <-- Data hidden
|-----------------------------|
| + deposit() (public) | <-- Accessible methods
| + withdraw() (public) |
+-----------------------------+
3.4 Abstraction
- Abstraction means showing only the essential features of an object and hiding the unnecessary implementation details.
- It reduces complexity and allows the programmer to focus on what an object does rather than how it does it.
- Example: When you call
car.start(), you don't need to know the internal combustion mechanism. - Achieved through abstract classes and interfaces.
3.5 Inheritance
- Inheritance is the mechanism by which one class (child/derived class) acquires the properties and behaviors of another class (parent/base class).
- Promotes code reusability and establishes an "is-a" relationship.
Types of Inheritance:
| Type | Description |
|---|---|
| Single | One child inherits from one parent |
| Multiple | One child inherits from multiple parents |
| Multilevel | Chain of inheritance (A -> B -> C) |
| Hierarchical | Multiple children from one parent |
| Hybrid | Combination of above types |
Animal (Base Class)
/ \
Dog Cat (Derived Classes)
3.6 Polymorphism
- Polymorphism means "many forms". It allows the same function or operator to behave differently based on the context.
- It enables one interface to be used for a general class of actions.
Two types:
| Type | Description | Example |
|---|---|---|
| Compile-time (Static) | Resolved at compile time | Function overloading, Operator overloading |
| Run-time (Dynamic) | Resolved at run time | Function overriding (virtual functions) |
Example:
// Function Overloading (Compile-time)
int add(int a, int b) { return a + b; }
float add(float a, float b) { return a + b; }
3.7 Message Passing
- Objects communicate with each other by sending and receiving messages (calling methods).
- A message specifies the name of the object, the method to invoke, and any parameters needed.
- Example:
s1.display();-- objects1receives the message to executedisplay().
3.8 Dynamic Binding
- Dynamic Binding (also called late binding) means the code to be executed in response to a function call is determined at runtime, not at compile time
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.