Ch 1. Introduction to Object Oriented Programming
3 hours · 4 marks
1.1 History of Programming Languages
Programming languages evolved through several generations, each solving problems of the previous one:
- Machine language (1GL): raw binary instructions understood directly by hardware. Fast but extremely hard to write and debug.
- Assembly language (2GL): mnemonic codes (MOV, ADD, JMP) mapped one-to-one to machine instructions. Easier than binary but still hardware-dependent.
- Procedural / structured languages (3GL): FORTRAN (1957), COBOL (1959), C (1972), Pascal — introduced control structures (loops, conditionals) and functions/procedures. Code is organized as a sequence of instructions operating on data.
- Object oriented languages: Simula 67 introduced the class concept; Smalltalk (1970s) was the first purely object oriented language; C++ (Bjarne Stroustrup, early 1980s, originally "C with Classes") added OOP to C; Java, C#, Python followed later.
The trend across generations has been increasing abstraction: from raw hardware instructions towards structures that reflect the real-world problem being solved.
1.2 Need of Object Oriented Programming (OOP)
As software grew larger in the 1970s–80s, purely procedural code became difficult to manage. Key drivers behind OOP:
- Data safety: in procedural code, data is global and any function can modify it, causing accidental corruption. OOP binds data with the functions that operate on it.
- Code reusability: OOP allows classes to be reused and extended (inheritance) instead of rewriting code.
- Modeling real-world entities: objects (Car, Employee, BankAccount) map naturally to real-world entities, making design and communication with stakeholders easier.
- Maintainability and scalability: encapsulation and modularity make large systems easier to extend and debug.
- Managing complexity: breaking a system into interacting objects, each responsible for its own state and behaviour, reduces overall complexity.
1.3 Object Oriented Programming versus Procedure Oriented Programming
| Aspect | Procedure Oriented (POP) | Object Oriented (OOP) |
| Primary focus | Functions/procedures | Objects (data + functions together) |
| Data | Global, moves freely between functions | Encapsulated inside objects, access controlled |
| Data security | Low — any function can change global data | High — data hidden via access specifiers |
| Program division | Divided into functions | Divided into objects/classes |
| Approach | Top-down | Bottom-up |
| Reusability | Limited (function reuse only) | High (inheritance, polymorphism) |
| Real world mapping | Poor | Natural / intuitive |
| Examples | C, Pascal, FORTRAN | C++, Java, C#, Python |
1.4 Concepts of Object Oriented Programming
Object
A run-time entity that bundles state (data/attributes) and behaviour (member functions/methods). Example: a Car object has state (color, speed) and behaviour (accelerate(), brake()).
Class
A blueprint/template for creating objects. It defines what data members and member functions objects of that type will have; no memory for data members is allocated until an object is created.
Encapsulation
Wrapping data and the functions that operate on it into a single unit (class), and restricting direct access to internal data using access specifiers (private, protected). This is also called data hiding.
Abstraction
Showing only essential features to the user while hiding implementation details. E.g., a driver uses the steering wheel and pedals (interface) without knowing the internal engine mechanics.
Inheritance
A mechanism where a new class (derived/child) acquires properties and behaviour of an existing class (base/parent), enabling code reuse and hierarchical classification (is-a relationship).
Polymorphism
"Many forms" — the ability of a function or operator to behave differently based on context. Two kinds:
- Compile-time (static) polymorphism: function overloading, operator overloading — resolved at compile time.
- Run-time (dynamic) polymorphism: achieved through virtual functions — resolved at run time using the actual object type.
Message passing
Objects communicate by invoking each other's member functions (sending messages), specifying the receiving object, the method name, and any information (arguments) needed.
1.5 Popular Object Oriented Languages
- Simula 67 — first language to introduce the class concept (used mainly for simulation).
- Smalltalk — first purely object oriented language; everything is an object.
- C++ — hybrid language: superset of C with OOP features added.
- Java — purely object oriented (except primitives), platform independent via JVM.
- C# — Microsoft's OOP language for the .NET platform.
- Python — multi-paradigm language with strong OOP support.
1.6 Advantages of OOP
- Better data security through encapsulation and access control.
- Code reusability via inheritance — reduces redundancy and development time.
- Easier maintenance — changes in one class have limited/controlled effect elsewhere.
- Real-world problem modeling is more natural and intuitive.
- Polymorphism allows flexible and extensible code design.
- Large software systems can be built and managed more easily through modular objects.
1.7 Disadvantages of OOP
- Steeper learning curve compared to procedural programming.
- Larger program size and can run slower due to abstraction overhead (e.g., virtual function calls).
- Not suitable for all types of problems — very simple, small, linear programs may be over-engineered as OOP.
- Requires careful upfront design (class hierarchies); a poor design is hard to refactor later.
- Programs may consume more memory (objects, vtables, etc.).
Most Probable Questions
Q1. Differentiate between procedure oriented programming and object oriented programming. 4 marks
Use the comparison table above — cover at least 4 points: focus (function vs object), data security (global/open vs encapsulated), approach (top-down vs bottom-up), and reusability (limited vs high via inheritance).
Q2. What is OOP? Explain the need for object oriented programming. 4 marks
Define OOP as a programming paradigm based on the concept of objects that bundle data and behaviour. Explain the need: managing growing software complexity, protecting data via encapsulation, enabling reusability through inheritance, and modeling real-world entities naturally (see section 1.2).
Q3. Explain the basic concepts (pillars) of object oriented programming with examples. 5 marks
Explain each of: class & object, encapsulation, abstraction, inheritance, polymorphism, message passing — one line definition + one example each (see section 1.4). Diagram: Base class → Derived class via inheritance arrow is a common way to illustrate this.
Q4. List the advantages and disadvantages of OOP. 4 marks
Advantages: reusability, data security, maintainability, real-world modeling, polymorphism. Disadvantages: learning curve, larger/slower programs, not always necessary for small problems (see sections 1.6–1.7).
Q5. What is encapsulation? How is it different from abstraction? 3 marks
Encapsulation is the mechanism (binding data + methods in a class and restricting access via private/protected). Abstraction is the concept/goal (hiding unnecessary implementation detail and exposing only what is needed). Encapsulation is often the technique used to achieve abstraction — e.g., a class exposes a public method deposit() while hiding the private balance variable and internal validation logic.
Q6. Differentiate between compile-time and run-time polymorphism. 4 marks
Compile-time (static) polymorphism is resolved by the compiler before execution — achieved via function overloading and operator overloading; binding is called early/static binding. Run-time (dynamic) polymorphism is resolved during program execution based on the actual object type — achieved via virtual functions; binding is called late/dynamic binding. (Full detail in Chapter 6.)
Q7. Name any four object oriented programming languages and mention one key feature of each. 4 marks
C++: hybrid language, superset of C. Java: platform independent, purely object oriented (runs on JVM). Python: dynamically typed, multi-paradigm with concise OOP syntax. C#: designed for the .NET framework, similar syntax to Java. (See section 1.5.)
Q8. Explain the evolution of programming languages leading to OOP. 4 marks
Trace: machine language → assembly language → procedural/structured languages (FORTRAN, COBOL, C) → object oriented languages (Simula 67, Smalltalk, C++, Java). Emphasize the driving theme: increasing abstraction and better handling of complexity as software grew larger (see section 1.1).