2078 2

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]

Object Oriented ProgrammingChapter 1: Object-Oriented Programming FundamentalsOOP definition and concepts

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

FeatureProcedure-Oriented (POP)Object-Oriented (OOP)
Basic UnitFunction / ProcedureObject
FocusAlgorithm / LogicData
Data AccessData is global, freely accessibleData is hidden (encapsulated)
ApproachTop-downBottom-up
Data SecurityLess secure (no data hiding)More secure (data hiding)
Code ReusabilityLimited (functions can be reused)High (via inheritance)
Real-world modelingDifficultNatural and easy
MaintenanceHarder for large programsEasier due to modularity
ExamplesC, Pascal, FORTRANC++, 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; -- here s1 is an object of class Student.

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:

TypeDescription
SingleOne child inherits from one parent
MultipleOne child inherits from multiple parents
MultilevelChain of inheritance (A -> B -> C)
HierarchicalMultiple children from one parent
HybridCombination 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:

TypeDescriptionExample
Compile-time (Static)Resolved at compile timeFunction overloading, Operator overloading
Run-time (Dynamic)Resolved at run timeFunction 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(); -- object s1 receives the message to execute display().

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.