Object - Oriented Programming (OOP)
Unit: 5
Object Oriented Programming (OOP)
Grade XII | Computer Science | NEB
Exam-focused notes with definitions, comparison tables, examples and model questions.
Exam pattern: This unit usually carries about 6 marks (a long answer or two short answers). Most repeated questions are: define OOP and its features, POP vs OOP, advantages and disadvantages, and short notes on class, object, encapsulation, inheritance and polymorphism.
Contents
- Programming approaches
- Procedure Oriented Programming (POP)
- Object Oriented Programming (OOP)
- Difference between POP and OOP
- Basic concepts of OOP
- Simple C examples
- Advantages and disadvantages of OOP
- Applications of OOP
- Important exam questions with answers
- Quick revision
1. Programming approaches
There are two main approaches to developing a program:
- Procedure Oriented Programming (POP): the traditional approach.
- Object Oriented Programming (OOP): the modern approach, developed to overcome the weaknesses of POP.
2. Procedure Oriented Programming (POP)
Definition: POP is a programming approach in which a large program is divided into small parts called functions (procedures). The focus is on the sequence of tasks (functions), not on the data.
Data is stored in variables that are either global (declared in the main module and accessible from any function) or local (declared inside a function and accessible only there). Because many functions can reach global data, it can be changed accidentally.
Features of POP
- A large program is broken into small procedures or functions.
- Emphasis is on functions rather than data.
- Data is declared as local or global.
- Global data can be changed by any function, so data security is low.
- It follows the top-down approach.
Examples: FORTRAN, COBOL, Pascal, C.
3. Object Oriented Programming (OOP)
Definition: OOP is a programming approach in which a program is organised around objects, which combine data and the functions that work on that data into one unit. Real-world entities are modelled as objects, and similar objects are grouped into classes.
OOP gives priority to data. Data can be hidden from outside code, so the chance of accidental change is much smaller than in POP.
Characteristics (features) of OOP
- Emphasis is on data rather than procedures.
- A program is divided into objects.
- Data and functions are bound together in a single unit (class).
- Data can be hidden to prevent accidental modification.
- It follows the bottom-up approach.
- Objects communicate with each other by passing messages.
- New data and functions can be added easily, and existing code can be reused through inheritance.
Examples: C++, Java, Python, C#, Smalltalk.
4. Difference between POP and OOP
| Basis | Procedure Oriented (POP) | Object Oriented (OOP) |
|---|---|---|
| Emphasis | On procedures (functions) | On data |
| Program division | Divided into functions or modules | Divided into objects |
| Approach | Top-down | Bottom-up |
| Data hiding | Generally not possible; data is open to all functions | Possible using access specifiers (private, protected) |
| Data security | Less secure | More secure |
| Real-world modelling | Does not model the real world well | Models real-world entities closely |
| Code reuse | Difficult | Easy, through inheritance |
| Maintenance | Difficult for large programs | Easier |
| Overloading | Not possible | Possible (function and operator overloading) |
| Examples | C, FORTRAN, COBOL, Pascal | C++, Java, Python, C# |
5. Basic concepts of OOP
a) Object
An object is a real-world entity that has attributes (data) and behaviour (functions). It is an instance of a class.
Examples of objects: a student, a book, a car, a bank account. For a car object, colour, size, weight are attributes, and move, turn, stop are behaviours.
b) Class
A class is a user-defined data type that acts as a blueprint (template) for creating objects. It defines the data members and member functions that its objects will have.
Example: the class Vehicle can have objects such as car, bus and truck. The class School can have objects such as student, teacher and staff. Once a class is defined, any number of objects can be created from it.
Remember: A class is only a description and uses no memory for data until objects are created. An object is the real instance that occupies memory.
c) Data abstraction
Abstraction means showing only the essential features to the user and hiding the internal details.
Example: when you drive a car you use the steering wheel, brake and accelerator without knowing how the engine works. In programming, a user calls a function without needing to know how it is written inside. Abstraction makes software easier to use and protects data from accidental change.
d) Encapsulation
Encapsulation is the process of wrapping data and the functions that operate on it into a single unit called a class.
Data is kept private so it can only be accessed through the class's own functions. This gives data hiding: outside functions cannot use or change the data directly.
Difference: abstraction hides implementation details (design level), while encapsulation hides data by binding it with functions (implementation level).
e) Inheritance
Inheritance is the process by which a new class (derived, child or sub class) acquires the properties and behaviour of an existing class (base, parent or super class).
- It supports code reusability: existing code is reused without rewriting it.
- The derived class can also add its own new members.
- It represents real-world hierarchy, for example Person тЖТ Student or Vehicle тЖТ Car.
Types of inheritance: single, multilevel, multiple, hierarchical and hybrid.
f) Polymorphism
Polymorphism (poly = many, morph = forms) is the ability of an operator or function to behave differently in different situations.
Example: the + operator adds two numbers (5 + 3 = 8) but joins two strings ("Ram" + "Sharma"). A function named area() can calculate the area of a circle or a rectangle depending on the arguments given.
Types:
- Compile-time polymorphism: function overloading and operator overloading.
- Run-time polymorphism: function overriding (a child class changes how an inherited function works).
g) Dynamic binding
Binding means linking a function call to the code that will run. In dynamic (late) binding this link is made at run time, not at compile time. It is closely related to run-time polymorphism: the function that runs depends on the type of object at that moment.
h) Message passing
Objects communicate by sending and receiving information, called messages. A message names the object, the function to run and the information needed. Example: student1.display(); sends a message to the object student1 to run its display() function.
6. Simple C examples
You already know C programming, so these two small programs help you understand the difference between POP and OOP. Remember: C is a procedure oriented language. It does not have classes or inheritance, but these examples show why OOP was needed.
Example 1: POP style (data is open to every function)
#include <stdio.h>
int length = 10; /* global variable */
void area()
{
printf("Area = %d\n", length * 5);
}
void change()
{
length = 99; /* any function can change global data */
}
int main()
{
area();
change();
area();
return 0;
}
Output:
Area = 50
Area = 495
Explanation: The function change() altered the global variable length, so the second answer is different. In POP, data is not protected and any function can change it by mistake. This is the main weakness of POP.
OOP solves this problem by hiding data inside a class, so only the functions of that class can change it.
Example 2: Grouping related data (the idea behind a class)
#include <stdio.h>
struct Student
{
int roll;
char name[20];
float marks;
};
int main()
{
struct Student s1 = {1, "Sita", 85.5};
struct Student s2 = {2, "Ram", 78.0};
printf("Roll: %d, Name: %s, Marks: %.1f\n", s1.roll, s1.name, s1.marks);
printf("Roll: %d, Name: %s, Marks: %.1f\n", s2.roll, s2.name, s2.marks);
return 0;
}
Output:
Roll: 1, Name: Sita, Marks: 85.5
Roll: 2, Name: Ram, Marks: 78.0
Explanation: The structure Student keeps the data of one student together in one unit. In the same way, a class is a blueprint and s1 and s2 are like objects made from it.
A C structure can only store data. It cannot keep functions inside it and cannot hide its data. An OOP class can do both: it puts data and functions together (encapsulation) and hides the data from outside.
Everyday examples for the other concepts
| Concept | Everyday example |
|---|---|
| Class and object | Mobile phone is a class; your phone and your friend's phone are objects. |
| Abstraction | You use a TV remote without knowing the circuit inside it. |
| Encapsulation | A medicine capsule wraps the medicine inside one cover. |
| Inheritance | A child inherits features such as eye colour from parents. |
| Polymorphism | The word "run" means different things for a person, a program and a tap. |
7. Advantages and disadvantages of OOP
Advantages
- Reduces code repetition through inheritance.
- Data is more secure because of data hiding and abstraction.
- Existing classes can be reused as library classes.
- Dividing a program into objects makes development easier.
- Reduces software complexity.
- Upgrading and maintenance are easy.
- Models real-world systems closely.
- Code reusability is easier than in traditional programming.
- Supports teamwork, as different objects can be built by different programmers.
Disadvantages
- Compiler and run-time overhead is higher, so programs may run slower and use more memory.
- The programmer must analyse the problem in an object-oriented way.
- Requires good knowledge of software engineering and programming methodology.
- Not well suited to small, simple problems; it is most useful for large and complex projects.
- Takes more time to learn, especially for beginners.
8. Applications of OOP
- Expert systems
- Artificial intelligence
- Management information systems
- Decision support systems
- Computer-based training and education
- Object-oriented databases
- Computer games
- Mobile applications
- Internet and web-based applications
- Designing user interfaces (GUI) for software
- Security systems
9. Important exam questions with answers
Try each question yourself first, then read the answer.
Q1. What is OOP? Describe its features. (6 marks)
OOP is a programming approach in which a program is built around objects that combine data and functions. Real-world entities are represented as objects, and similar objects are grouped into classes.
Features: (1) emphasis on data, (2) program divided into objects, (3) data and functions bound in one unit, (4) data hiding, (5) bottom-up approach, (6) message passing between objects, (7) reusability through inheritance, (8) polymorphism. Explain any of the concepts (class, object, abstraction, encapsulation, inheritance, polymorphism) in two or three lines each.
Q2. Write the advantages and disadvantages of OOP. (6 marks)
Write any 4 advantages and 3 to 4 disadvantages from Section 7 with one line of explanation each.
Q3. Explain polymorphism and inheritance. (4 marks)
Polymorphism means one name with many forms: the same operator or function acts differently in different situations, such as + adding numbers and joining strings. It is of two types, compile-time (overloading) and run-time (overriding).
Inheritance is the process of creating a new class from an existing class so that the new class acquires its properties and behaviour. It promotes code reuse. Example: Student inherits from Person.
Q4. Differentiate between OOP and structured (procedural) programming. (4 to 5 marks)
Write any five rows from the table in Section 4: emphasis, program division, approach, data hiding, reusability, real-world modelling, with examples of languages.
Q5. Explain class, object and encapsulation. (6 marks)
Give the definition of each, one real-life example each (class Vehicle, object car, encapsulation as a capsule wrapping medicine), and mention that objects are created from a class and that encapsulation gives data hiding. Using the Student example from Section 6 as an illustration earns extra credit.
Q6. Differentiate between abstraction and encapsulation. (3 marks)
Abstraction shows only essential details and hides the implementation; it is achieved at design level. Encapsulation wraps data and functions into one unit and restricts direct access to data; it is achieved at implementation level through classes and access specifiers.
Q7. Differentiate between a class and an object. (3 marks)
A class is a blueprint or user-defined data type, logical in nature, and does not occupy memory for data. An object is an instance of a class, physical in nature, and occupies memory. Many objects can be created from one class.
Q8. Write short notes on dynamic binding and message passing. (4 marks)
Use the explanations in Section 5 (g) and (h). Add the example student1.display(); for message passing.
10. Quick revision
| Term | One-line meaning |
|---|---|
| Object | Instance of a class with data and behaviour |
| Class | Blueprint or template for objects |
| Abstraction | Show essential features, hide details |
| Encapsulation | Bind data and functions in one unit |
| Inheritance | Child class acquires features of parent class |
| Polymorphism | One name, many forms |
| Dynamic binding | Function call linked to code at run time |
| Message passing | Objects communicate by calling each other's functions |
Prepared for Grade XII Computer Science (NEB). Always cross-check with the latest syllabus and textbook issued by the Curriculum Development Centre (CDC).