Chapter 1: Introduction To Object-Oriented Programming
Chapter 1: Introduction To Object-Oriented Programming
Chapter 1: Introduction to
object-oriented programming
Department of Computer Science and Engineering
Kathmandu University
Programming Paradigm
A way to classify programming languages based on their features.
● Imperative programming
○ The programmer specifies exactly how to do something, not just the desired outcome.
○ Procedural programming (C, C++ etc.) / object-oriented programming (C++, Java etc.)
● Declarative programming
○ The programmer declares what needs to happen, not how it’s done.
○ SQL, HTML, Prolog, Common LISP, Clojure etc.
2
COMP 116
Procedural programming
In “procedural” programming, problems are “decomposed” into smaller units of
repeatable activities called procedures.
Main Program
Function-1 Function-3
Function-2
Function-4 Function-5
3
COMP 116
Procedural programming
In “procedural” programming, problems are “decomposed” into smaller units of
repeatable activities called procedures.
4
COMP 116
Procedural programming
Disadvantages
● Focus on processes
○ Importance is given to the operation on data rather than the data.
● No information hiding
○ Data is exposed to the whole program.
● Difficult to manage large programs
○ Imagine changing the type of a global variable from int to long in a large program!!
● Difficult to relate with real-world objects
○ In physical world, we deal with objects such as people, cars etc. Such objects have both attributes
(data) and behavior (functions).
○ Neither data nor functions, by themselves, model real-world objects effectively.
5
COMP 116
6
COMP 116
Procedural OOP
7
COMP 116
Object-Oriented Programming
Suppose you want to build a library management system.
8
COMP 116
Some examples:
● Human entities
○ Employees, Students, Customers etc.
● Physical objects
○ Cars, planes, buses, countries, house, electrical components, food items (pizza, noodles) etc.
● Elements of the computer-user environment
○ Windows, menus, graphic objects (lines, rectangles, circles), mouse, keyboard etc.
● Data-storage constructs
○ Customized arrays, stacks, linked lists, binary trees etc.
● User-defined data types
○ Time, angles, complex numbers etc.
● etc. 9
COMP 116
10
COMP 116
Data encapsulation:
● Restricting direct access to the data (so that it is safe from accidental
alteration).
● Only those functions which are wrapped in the class can access it.
● This simplifies writing, debugging, and maintaining the program.
11
COMP 116
12
COMP 116
Its main goal is to handle complexity by hiding unnecessary details from the user.
Examples:
● While using mobile phone, we know what happens when we click on home
button, back button and many more. But unknown about internal working
mechanism.
● Your program can make a call to the sort() function without knowing which
algorithm the function actually uses to sort the given values.
13
COMP 116
14
COMP 116
15
COMP 116
16
COMP 116
Benefits of OOP
Some potential benefits are
● Reusability
○ Once a class has been written, created, and debugged, it can be distributed to other programmers
for use in their own programs.
● Robustness
○ Most object-oriented languages support exception and error handling
● Extensibility
○ A programmer can take an existing class and, without modifying it, add additional features and
capabilities to it (thanks to the inheritance).
● Easier to manage
○ Each object is relatively small, self-contained, and manageable, thus reducing the complexity of
the program.
17