Unit 1: Introduction to Object Oriented Programming [3 Hours]
Object Oriented Programming (OOP) is a programming approach that organizes software around objects containing data and operations. This unit introduces structured and object oriented programming approaches, their differences, and the fundamental characteristics of object oriented languages.
Tested Topics
- Characteristics of Object Oriented Languages — 2x (2081, 2079)
1.1 Overview of Structured and Object Oriented Programming Approach
A programming paradigm is a fundamental approach or style used to design, organize, and develop computer programs. Structured programming and object oriented programming are two widely used programming approaches that differ primarily in how they organize data, operations, and program responsibilities.
1. Structured Programming Approach
Structured programming is a programming approach in which a complex program is divided into smaller, manageable functions or procedures using well-defined control structures to improve readability, reliability, and maintainability.
It focuses on the sequence of operations required to solve a problem. A large problem is divided into smaller problems, and each smaller problem is solved using an appropriate function or procedure.
Structured programming commonly follows a top-down approach, where program development begins with the overall problem and gradually breaks it into smaller functional components.
Basic Control Structures
Structured programming is based on three fundamental control structures:
- Sequence: Instructions are executed one after another in a specified order.
- Selection: A particular set of instructions is executed depending on a condition, such as an if-else statement.
- Iteration: A set of instructions is executed repeatedly using loops such as for and while.
Characteristics of Structured Programming
- Emphasizes functions, procedures, and algorithms.
- Commonly follows a top-down design approach.
- Divides a large program into smaller functional modules.
- Generally organizes data separately from the functions that operate on it.
- Uses sequence, selection, and iteration to control program execution.
- Reduces unnecessary and unstructured jumps between instructions.
- Supports code reuse through functions and modules.
Example: Bank Account Management
Consider a banking program that performs deposits, withdrawals, and balance inquiries. In structured programming, these operations can be implemented as separate functions that receive or access account data.
The following language-neutral pseudocode illustrates a simple deposit operation:
balance = 5000
function deposit(amount):
balance = balance + amount
deposit(1000)
print(balance)Output:
6000Here, the deposit function operates on data stored separately from the function. In larger programs, functions may receive data through parameters or operate on shared data, depending on the program design.
Advantages of Structured Programming
- Programs are easier to read and understand than unstructured programs.
- Division into functions makes testing and debugging easier.
- Functions can be reused in different parts of a program.
- Provides a clear and systematic approach to problem-solving.
- Suitable for tasks that are naturally expressed as sequences of operations.
Limitations of Structured Programming
- Separating data and operations can make their relationships harder to manage in large applications.
- Excessive use of shared data may lead to unintended modifications and dependencies between functions.
- Changes to a shared data structure may require changes in several functions.
- Representing complex real-world entities may be less natural than in an object oriented design.
- Does not inherently provide class-based inheritance and object-oriented polymorphism.
2. Object Oriented Programming Approach
Object Oriented Programming (OOP) is a programming paradigm in which a program is organized around objects that combine data and the methods required to operate on that data.
Instead of concentrating primarily on functions, OOP focuses on identifying the entities involved in a problem, their properties, their behavior, and the relationships between them.
OOP is commonly associated with a bottom-up approach, where smaller, reusable components are developed and combined to construct a larger system. However, object oriented design can also use top-down planning.
Basic Organization of an Object Oriented Program
- Class: Defines the common properties and behavior of a group of objects.
- Object: An individual instance of a class containing its own state and providing defined behavior.
- Data members: Variables that represent the properties or state of an object.
- Member functions or methods: Operations that define what an object can do.
In structured programming, the program is primarily organized around functions operating on data. In OOP, related data and operations are grouped into objects, giving each object responsibility for managing its own state.

Example: Bank Account Management
In an object oriented banking program, a BankAccount class can define account information and operations such as depositing money, withdrawing money, and checking the balance. Each account is represented by a separate object.
Class: BankAccount
Data:
accountNumber
balance
Methods:
deposit(amount)
withdraw(amount)
getBalance()Suppose two objects, accountA and accountB, are created from the BankAccount class.
| Property | accountA | accountB |
|---|---|---|
| Account number | 101 | 102 |
| Initial balance | Rs. 5,000 | Rs. 8,000 |
| Operation | deposit(1000) | No operation |
| Final balance | Rs. 6,000 | Rs. 8,000 |
Both objects have the same types of properties and operations because they belong to the same class. However, each maintains its own account information. Depositing money into accountA does not change the balance of accountB.
Function Calls and Object Interactions
In structured programming, a main procedure may call several functions to perform a task. In OOP, objects commonly interact by invoking one another's methods, requesting the object responsible for particular data to perform the required operation.
For example, a customer object may request an account object to withdraw money. The account object can check its own balance, validate the request, and perform the withdrawal if permitted.

Characteristics of the Object Oriented Approach
- Organizes programs around classes and objects.
- Combines related data and operations into a single unit.
- Supports controlled access to internal object data.
- Encourages modularity and code reusability.
- Allows objects to interact through method calls.
- Supports inheritance, abstraction, encapsulation, and polymorphism.
- Provides a natural way to model real-world entities and their relationships.
Advantages of Object Oriented Programming
- Code reusability: Existing classes and components can be reused instead of developing similar functionality repeatedly.
- Data protection: Access to internal data can be controlled through defined interfaces and access restrictions.
- Modularity: A large program can be divided into smaller classes and objects with specific responsibilities.
- Maintainability: Changes to the internal implementation of a class can often be made without affecting other components that use its interface.
- Extensibility: New functionality can be introduced through new classes, composition, inheritance, or polymorphism.
- Real-world modeling: Entities such as students, employees, vehicles, and bank accounts can be represented naturally as objects.
- Flexibility: Different objects can provide different implementations of a common operation.
Limitations of Object Oriented Programming
- Requires understanding additional concepts such as classes, inheritance, and polymorphism.
- Designing appropriate classes and relationships may require more initial planning.
- May introduce unnecessary complexity in very small programs.
- Some implementations may require additional memory or processing overhead.
- Poorly designed class relationships can create unnecessary dependencies and make maintenance difficult.
Difference Between Structured and Object Oriented Programming
| Basis | Structured Programming | Object Oriented Programming |
|---|---|---|
| Primary focus | Functions, procedures, and algorithms. | Objects, their state, and behavior. |
| Design approach | Commonly follows a top-down approach. | Commonly associated with a bottom-up approach. |
| Basic program unit | Function or procedure. | Class and object. |
| Data organization | Data and functions are generally organized separately. | Related data and methods are grouped together. |
| Data protection | Can use scope and module boundaries, but does not inherently provide object-level access control. | Supports controlled access through encapsulation and access restrictions. |
| Communication | Function calls and parameter passing. | Objects interact primarily through method calls and message passing. |
| Code reusability | Through functions, procedures, and modules. | Through classes, composition, inheritance, and reusable methods. |
| Inheritance | Not an inherent feature. | A common mechanism for reuse and specialization. |
| Polymorphism | Object-oriented subtype polymorphism is not an inherent feature. | Supports common interfaces with different implementations. |
| Real-world modeling | Models problems primarily through operations. | Models problems through interacting entities. |
| Maintenance | Shared data dependencies can complicate maintenance as programs grow. | Well-designed class boundaries can localize changes. |
| Language examples | C, Pascal. | C++, Java, C#, Python. |
Common Mistake
Structured programming and OOP are not mutually exclusive. Object oriented programs also use functions, conditions, loops, and structured control flow. The primary difference lies in how the overall program is organized. Languages such as C++ and Python support multiple programming paradigms.
Summary
- Structured programming organizes a solution around functions and procedures.
- Sequence, selection, and iteration are the fundamental structured control mechanisms.
- OOP organizes a solution around objects containing related data and behavior.
- Both approaches support modularity and reuse, but use different organizational mechanisms.
- OOP introduces object-based mechanisms for data protection, specialization, and flexible behavior.
Test Yourself
- Define structured programming and explain its basic control structures.
- Differentiate between structured programming and object oriented programming.
- Explain how a banking system can be organized using the object oriented approach.
Hints:
- Function-centered decomposition; sequence, selection, and iteration.
- Compare focus, program organization, data access, design approach, inheritance, and reusability.
- BankAccount class, individual account objects, separate balances, and methods such as deposit() and withdraw().
1.2 Characteristics of Object Oriented Languages
Object oriented languages provide mechanisms for representing entities as objects, combining data and behavior, controlling access to information, reusing existing components, and allowing objects to interact with one another.
The fundamental characteristics of object oriented programming are classes, objects, encapsulation, data hiding, abstraction, inheritance, polymorphism, dynamic binding, and message passing.
1. Class
A class is a user-defined type or blueprint that specifies the data members and methods common to a group of objects.
A class describes what information its objects can contain and what operations they can perform. Individual objects created from the class can have different values for their data members.
For example, a Student class may contain:
- Data members: name, rollNumber, and marks.
- Methods: displayDetails(), calculateGrade(), and updateMarks().
The Student class provides a common structure that can be used to represent many individual students without defining a separate structure for every student.
2. Object
An object is an instance of a class that possesses its own identity, state, and behavior.
An object represents a particular entity in a program. Multiple objects can be created from the same class, with each object maintaining its own state.
Every object has three fundamental aspects:
- Identity: Distinguishes an object from other objects, even when their data values are identical.
- State: Represents the current values of the object's data members.
- Behavior: Represents the operations that the object can perform through its methods.
For example, two students represented using the Student class may have the following states:
| Property | student1 | student2 |
|---|---|---|
| Name | Ram | Sita |
| Roll number | 1 | 2 |
| Marks | 75 | 85 |
| Behavior | displayDetails(), calculateGrade() | displayDetails(), calculateGrade() |
Both objects belong to the same class and provide the same types of operations, but their identities and data values are different.
Difference Between Class and Object
| Class | Object |
|---|---|
| A blueprint or user-defined type. | An instance of a class. |
| Defines properties and methods. | Contains actual instance data and provides the defined behavior. |
| Represents a general category of entities. | Represents an individual entity. |
| One class can define many objects. | Each object has its own identity. |
| Example: Student. | Example: student1, student2. |
3. Encapsulation
Encapsulation is the mechanism of combining data and the methods that operate on that data into a single unit, usually a class, while providing controlled access to the internal state.
Encapsulation establishes a boundary around related data and operations. Instead of allowing every part of a program to modify an object's data directly, the object can provide methods through which permitted operations are performed.

Example
A BankAccount class may store its balance as private data and provide public methods for depositing and withdrawing money.
Class: BankAccount
Private data:
balance
Public methods:
deposit(amount)
withdraw(amount)
getBalance()If an external component wants to withdraw money, it must use the withdrawal method rather than directly changing the balance.
The withdrawal method can check whether the requested amount is positive and whether sufficient funds are available before modifying the balance.
This helps ensure that the object's data remains in a valid state.
Benefits of Encapsulation
- Groups related data and operations together.
- Prevents unintended direct modifications to internal data when access is restricted.
- Allows validation before changing an object's state.
- Reduces dependencies between different program components.
- Improves modularity and maintainability.
4. Data Hiding
Data hiding is the mechanism of restricting direct access to the internal data of a class so that it can be accessed or modified only through permitted operations.
Many object oriented languages provide access modifiers to control the visibility of class members.
- Private: Restricts access primarily to the class that defines the member, subject to the language's rules.
- Protected: Generally permits access within the class and its subclasses, with additional rules depending on the language.
- Public: Makes the member accessible to external code through the class or object interface.
For example, storing a bank account balance as a private member and exposing only permitted deposit and withdrawal methods prevents ordinary external code from assigning an arbitrary balance directly.
Encapsulation and data hiding are closely related. Encapsulation groups data and behavior together, while data hiding restricts access to internal information.
5. Abstraction
Abstraction is the process of representing the essential features of an entity while hiding unnecessary implementation details from the user.
Abstraction allows a user or another program component to know what an operation does without needing to understand every detail of how it works internally.
An interface describes the operations available to a caller. The implementation contains the internal instructions, algorithms, and data handling required to perform those operations.

Example
Consider the withdraw(amount) method of a BankAccount object. The caller needs to know the operation's purpose, the required amount, and the possible result.
The caller does not need to understand every internal step, such as checking the balance, validating the transaction, updating records, and calculating the remaining balance.
The withdrawal method exposes the necessary functionality while hiding its implementation complexity.
Let's Clarify
When riding a scooter, a rider uses the accelerator and brakes without needing to understand every mechanical process occurring inside the engine.
The controls expose the essential operations, while the internal mechanical processes remain hidden. Similarly, abstraction provides a useful interface without exposing unnecessary implementation details.
Benefits of Abstraction
- Reduces the complexity visible to the user.
- Allows developers to focus on essential functionality.
- Separates the use of an operation from its implementation.
- Allows internal implementation to change without necessarily changing the external interface.
Difference Between Abstraction and Encapsulation
| Basis | Abstraction | Encapsulation |
|---|---|---|
| Meaning | Represents essential features while hiding unnecessary implementation details. | Combines related data and methods into a single unit with controlled access. |
| Primary purpose | Reduce visible complexity. | Protect and manage internal state. |
| Focus | What an object provides. | How an object's data and operations are organized and accessed. |
| Example | Using withdraw() without knowing its internal implementation. | Keeping balance private and modifying it through permitted methods. |
6. Inheritance
Inheritance is a mechanism through which a new class is defined from an existing class, allowing it to acquire accessible properties and behavior and introduce or specialize functionality.
The existing class is called the base class, parent class, or superclass. The newly defined class is called the derived class, child class, or subclass.
Inheritance commonly represents an IS-A relationship between classes. For example, a car is a vehicle, and a bike is a vehicle.

Example
Consider a Vehicle class that defines common properties and operations for different vehicles.
Class: Vehicle
Properties:
speed
fuelLevel
Methods:
start()
stop()
Class: Car inherits Vehicle
Additional operation:
openTrunk()
Class: Bike inherits Vehicle
Additional operation:
applyStand()Both Car and Bike can reuse the common start() and stop() operations defined by Vehicle. Each derived class can also define behavior specific to its own requirements.
Whether a derived class can directly access inherited data depends on the access rules of the programming language.
Benefits of Inheritance
- Code reusability: Existing functionality can be reused in derived classes.
- Reduced duplication: Common behavior can be defined in one parent class.
- Extensibility: Derived classes can introduce additional properties and operations.
- Specialization: General classes can be extended to represent more specific entities.
- Maintainability: Improvements to suitable shared functionality can benefit multiple derived classes.
7. Polymorphism
Polymorphism is the ability of a common operation or interface to represent different forms of behavior depending on the object or arguments involved.
The term polymorphism means many forms. In object oriented programming, it allows the same method call or interface to produce different behavior in different contexts.
Example
Suppose Circle and Rectangle are different types of Shape. Both provide a draw() operation, but each requires a different implementation.
- Calling draw() on a Circle object draws a circle.
- Calling draw() on a Rectangle object draws a rectangle.
A caller can work with the common Shape interface without needing separate calling logic for every shape.

Types of Polymorphism
In class-based languages such as C++ and Java, polymorphism is commonly explained using compile-time and runtime polymorphism.
a. Compile-Time Polymorphism
Compile-time polymorphism, also called static polymorphism, occurs when the appropriate operation is determined during compilation.
A common example is function or method overloading, where functions have the same name but different parameter lists.
add(integer, integer)
add(decimal, decimal)
add(integer, integer, integer)The compiler selects an appropriate overloaded function based on the arguments and the language's overload resolution rules.
Operator overloading, where supported by the language, is another common example of compile-time polymorphism.
b. Runtime Polymorphism
Runtime polymorphism, also called dynamic polymorphism, occurs when the appropriate implementation of an operation is determined during program execution.
It is commonly achieved through method overriding and dynamic dispatch.
In method overriding, a derived class provides its own implementation of an inherited operation. When an overridable method is called through a compatible base-class reference, the actual object's type determines the implementation executed.
Shape reference = Circle object
reference.draw()
Shape reference = Rectangle object
reference.draw()Conceptual output:
Drawing a circle
Drawing a rectangleThe same method call produces different behavior because the reference refers to different types of objects.
| Compile-Time Polymorphism | Runtime Polymorphism |
|---|---|
| Operation selection occurs during compilation. | Implementation selection occurs during execution. |
| Also called static polymorphism. | Also called dynamic polymorphism. |
| Commonly achieved through overloading. | Commonly achieved through overriding and dynamic dispatch. |
| Selection commonly depends on the declared argument types and parameter lists. | Selection depends on the actual type of the receiving object. |
| Example: add() with different parameter lists. | Example: draw() implemented differently by Circle and Rectangle. |
Benefits of Polymorphism
- Allows different objects to be accessed through a common interface.
- Improves program flexibility and extensibility.
- Reduces the need for separate calling logic for every object type.
- Allows new compatible object types to be introduced without extensively modifying existing code.
8. Dynamic Binding
Dynamic binding, also called late binding, is the mechanism of determining which implementation of a method should be executed at runtime rather than at compile time.
Binding refers to associating a function or method call with its corresponding implementation.
There are two commonly discussed forms of binding:
- Static binding (early binding): The target operation is determined before program execution, commonly during compilation.
- Dynamic binding (late binding): The target method implementation is determined during program execution using the actual object involved.
Example
Consider an Animal class containing a sound() method. Its derived classes Dog and Cat provide their own implementations of that method.
Animal reference = Dog object
reference.sound()
Animal reference = Cat object
reference.sound()Conceptual output:
Bark
MeowAlthough both calls use the Animal reference and the same sound() operation, dynamic binding selects the appropriate implementation according to the actual object.
Dynamic binding is an important mechanism for achieving runtime polymorphism.
Common Mistake
Polymorphism and dynamic binding are related but not identical. Polymorphism is the ability of a common interface to exhibit different behavior. Dynamic binding is the runtime mechanism that selects the appropriate method implementation.
9. Message Passing
Message passing is the mechanism through which objects communicate and request services from one another, commonly by invoking methods and passing the required arguments.
A typical method-based message contains three elements:
- Receiver: The object that receives the request.
- Method name: The operation requested from the object.
- Arguments: The information required to perform the operation, if any.
Example
account.deposit(1000)- account: The receiving object.
- deposit: The requested operation.
- 1000: The argument supplied to the method.
The account object receives the request, executes the deposit operation, and updates its own balance according to its internal rules.
Similarly, a call to account.getBalance() requests the current balance, which may be returned to the caller as a result.
Benefits of Message Passing
- Enables interaction and cooperation between objects.
- Allows objects to request operations without directly manipulating another object's internal data.
- Supports modularity and separation of responsibilities.
- Helps maintain well-defined interfaces between program components.
Supporting Characteristics of Object Oriented Programming
The fundamental mechanisms of OOP also contribute to several broader software design characteristics:
- Modularity: A complex program can be divided into independent or loosely connected classes with specific responsibilities.
- Reusability: Existing classes, methods, and components can be reused through inheritance, composition, and other mechanisms.
- Extensibility: New classes and behavior can be added to an existing system without unnecessarily redesigning the entire program.
- Maintainability: Well-defined interfaces and class boundaries help developers locate, understand, and modify program components.
Relationship Between the Fundamental Characteristics of OOP
The characteristics of OOP work together rather than functioning as isolated concepts. Consider an object oriented banking application:
| Characteristic | Application in a Banking System |
|---|---|
| Class | BankAccount defines common account properties and operations. |
| Object | Individual customer accounts are represented as separate objects. |
| Encapsulation | Account information and related operations are grouped into a class. |
| Data hiding | External components cannot directly modify a private balance. |
| Abstraction | Users perform transactions without knowing their internal implementation. |
| Inheritance | SavingsAccount and CurrentAccount reuse suitable functionality from BankAccount. |
| Polymorphism | Different account types can implement withdraw() according to their respective rules. |
| Dynamic binding | The appropriate overridden withdrawal method is selected at runtime. |
| Message passing | A customer object requests account operations through method calls. |
Summary
- Classes and objects provide the fundamental structure of an object oriented program.
- Encapsulation and data hiding organize and protect internal object state.
- Abstraction exposes essential functionality while hiding implementation complexity.
- Inheritance and polymorphism support reuse, specialization, and flexible behavior.
- Dynamic binding and message passing support runtime method selection and communication between objects.
Test Yourself
- Differentiate between a class and an object with an example.
- Explain the difference between abstraction and encapsulation.
- Differentiate between compile-time and runtime polymorphism.
- How does dynamic binding support runtime polymorphism?
- Describe the features of object oriented programming. [5] (2081)
- Explain the features of OOP. (2079)
Hints:
- Class defines a common structure; an object is an individual instance. Example: Student and student1.
- Abstraction hides unnecessary implementation details; encapsulation groups related data and methods and controls access.
- Compile-time selection through overloading; runtime implementation selection through overriding and dynamic dispatch.
- The actual object's type determines which overridden method implementation is executed.
- Define OOP and describe classes, objects, encapsulation, data hiding, abstraction, inheritance, polymorphism, dynamic binding, and message passing.
- Explain the fundamental OOP characteristics, including their meanings and suitable examples. Connect them to reusability, modularity, data protection, and flexibility.
Exam Questions
- Describe the features of object oriented programming. [5] (2081)
- Explain the features of OOP. (2079)
Whole Chapter Summary
- Programming paradigm: A fundamental approach to designing and organizing computer programs.
- Structured programming: A function-centered approach that divides a problem into smaller procedures and uses structured control flow.
- Structured control structures: Sequence executes instructions in order, selection chooses between alternatives, and iteration repeats instructions.
- Object oriented programming: An object-centered approach that combines related data and behavior into interacting objects.
- Class: A blueprint or user-defined type describing the properties and behavior of objects.
- Object: An individual instance of a class possessing identity, state, and behavior.
- Encapsulation: Combines data and related methods into a single unit with controlled access.
- Data hiding: Restricts direct access to internal data.
- Abstraction: Represents essential functionality while hiding unnecessary implementation details.
- Inheritance: Allows a derived class to acquire and specialize functionality from an existing class.
- Polymorphism: Allows a common operation or interface to exhibit different forms of behavior.
- Compile-time polymorphism: Operation selection occurs during compilation, commonly through overloading.
- Runtime polymorphism: Method implementation selection occurs during execution, commonly through overriding and dynamic dispatch.
- Dynamic binding: Associates a method call with the appropriate implementation at runtime.
- Message passing: Enables objects to communicate and request operations from one another.
- Benefits of OOP: Modularity, reusability, maintainability, extensibility, controlled data access, and flexible behavior.
Last-Minute Revision Sheet
| Concept | Recall Keywords |
|---|---|
| Structured programming | Functions, procedures, top-down decomposition. |
| Basic control structures | Sequence, selection, iteration. |
| OOP | Objects, data + methods, interacting entities. |
| Class | Blueprint, common structure. |
| Object | Instance, identity, state, behavior. |
| Encapsulation | Bundling, controlled access. |
| Data hiding | Restricted visibility, private data. |
| Abstraction | Essential features, hidden implementation. |
| Inheritance | Parent → child, IS-A, reuse. |
| Polymorphism | One interface, many forms. |
| Compile-time polymorphism | Overloading, static selection. |
| Runtime polymorphism | Overriding, dynamic dispatch. |
| Dynamic binding | Runtime method selection. |
| Message passing | Receiver, method, arguments. |
| Modularity | Separate components, defined responsibilities. |
| Reusability | Reuse existing classes and functionality. |
| Extensibility | Add new functionality. |
Four Fundamental Pillars of OOP
- Encapsulation: Bundle and control access.
- Abstraction: Expose essentials and hide complexity.
- Inheritance: Reuse and specialize.
- Polymorphism: One interface with multiple forms of behavior.


