Computer Science Notes for Class 12 (ASSEB / AHSEC)
Chapter 1 - Object Oriented Programming (OOP)
Contents
Introduction
This section covers the basic concepts of OOP. Here we shall learn what is OOP, concept of class and objects, features of OOP, advantages of OOP and concept of abstract and concrete classes.
Object Oriented Programing
Object Oriented Programming (OOP) is a programming methodology in which programs are designed using objects rather than functions. Each object contains both data (attributes) and functions (methods).
Explanation: Suppose you want to build a School Management System. In traditional programming we mainly think about functions such as :
- Add student
- Calculate marks
- Calculate grade
In OOP, we first think about objects involved. Examples of objects may be:
- Student
- Teacher
- Book
Object
An object is an instance of a class. It represents a real-world entity that has certain characteristics (attributes) and behavior (functions).
Explanation: Consider a classroom with many students. Each student has certain attributes (data) like
- Name
- Rollno
- Marks
Each student has behaviour (what it can do) like
- Study
- Write Exam
- Attend class
Each individual student is an object.
Classes
A class is a collection of similar objects. It is a user-defined datatype that acts as a blueprint for creating objects.It contains data members and member functions.
Explanation: Before building a house, engineers prepare a design (blueprint) on paper.Using the same design (blueprint) multiple houses can be built.
Similarly, class is a blueprint and objects are created using that blueprint.
Example:
Class — Student
Object — Rahul, Priya, Amit
Features of OOP
The following are the features of OOP:
- Encapsulation
- Abstraction
- Modularity
- Inheritance
- Polymorphism
Encapsulation
The process of combining data and functions (that operate on data) into a single unit (called class) is called encapsulation.
Explanation: Think of a capsule medicine. A capsule contains different medicines packed together inside one covering.
Similarly, a class combines data and functions into one unit.This is called encapsulation.
Abstraction
Abstraction is the process of showing only the essential features of an object while hiding the unnecessary implementation details.
In othe words, abstraction focuses on what an object does, not how it does it.
Explanation: Imagine you are driving a car.
To drive the car, you only need to know:
- How to start the engine
- How to accelerate and brake
- How to steer
You do not need to know:
- How the engine burns fuel
- How the gearbox works
- How the braking system functions internally
The complex internal working is hidden from you.
This is called abstraction.
Modularity
Modularity is the process of dividing a large program into small independent modules, where each module performs a specific task.
Explanation: Let us consider a School Management System. Instead of writing everything on one huge program, we divide it into different modules. For example:
- Student Module
- Teacher Module
- Fee Module
- Library Module
- Examination Module
Each module performs its own work independently. This is modularity.
Inheritance
The process of creating a new class from an existing class is called Inheritance. The new class acquires the properties (data members) and behaviors (member functions) of the existing class and can add additional features of its own.
The existing class is called Base Class or Super Class and
the new class is called Derived Class or Sub Class.
Explanation: Consider a family.A child inherits characteristics from his / her parents. For example, eye color, hair color, height, etc.
Similarly, in programming a new class inherits the properties and functions of an existing class. Instead of writing the same code again, we simply inherit it. This saves time and effort.
Polymorphism
Polymorphism is the ability of an object, function or operator to take many forms. The word polymorphism comes from two Greek words:
- Poly means Many
- Morph means Forms
Explanation: Consider the word draw. When we say draw, we can
- Draw a circle
- Draw a triangle or
- Draw a rectangle
The word draw is same, but its meaning is different depending on the context. This is Polymorphism.
Data Hiding
Data hiding is the process of restricting direct access to the data members of a class. The data is protected from unauthorized access by declaring it as private and allowing access through only through public member functions.
In C++, data hiding is achieved by declaring data members as private.
Abstract Class
An Abstract class is a class whose objects cannot be created. It is designed to be used only as a base class for other classes. an abstract class usually contains one or more pure virtual functions.
Explanation: Imagine you are designing a building. Before construction begins, an architect prepares a blueprint / design.
The blueprint is not an actual building. You cannot live in it. It only provides a design based on which the buildings are constructed. Similarly,an abstract class is like a blueprint. You cannot create objects from it directly. Instead, other classes inherit from it and provide the missing details.
Concrete Class
A concrete class is a derived class of an abstract class whose objects can be created. It provides complete implementation of all member functions, including any inherited pure virtual function.
Explanation: Consider a car manufacturing company. The design is like an abstract class. The actual car that you buy and drive is like a concrete class.
Example: We may create a class named Vehicle that only defines common rules. Specific vehicles like car, bike, bus provide actual implementation. These are concrete classes.
Quick Revision
- OOP is a programming methodology in which programs are designed using objects rather than functions
- An object is an instance of a class.
- Class is a user defined data type that acts as a blueprint for creating objects
- Combining data and functions (that operate on data) into a single unit (called class) is called encapsulation.
- Showing only the essential features of an object while hiding the unnecessary implementation details is abstraction
- The process of creating a new clas from an existing class is called inheritance
- Polymorphism is the ability of an object, function or operator to take many forms.