Chapter 1 Introduction To OOP
Chapter 1 Introduction To OOP
✓ What is programing
✓ Programming Paradigm
✓ What is OOP
✓ Benefit of OOP
Basics of programming
✓ Computer is a device that performs tasks based on given set of step-by-step
instructions (procedures)
✓ These set of procedures (step-by-step instructions) are called algorithms also called
computer programs or soft wares.
✓ The way in which we tell/give the set of procedures or programs to a computer are
called programming languages. Examples are: C++, java, C#..
✓ The first thing with which one is confronted when writing programs is the problem
✓ We will interact with real world problem and we want to make life easier by providing a
program for the problem.
✓ The model focuses only on problem related stuff and that you try to define properties
of the problem
Daniel K OOP. 1
✓ In order to write a program, you need to go through several stages. These are
1. Type the text of the program into the computer
2. Translate the program text into a form the computer can use
Daniel K OOP. 3
To create a better solution for a problem, first you have to identify the project’s
requirement (defining what is the system supposed to do) and develop a design that
satisfies them (specifying how the system should do it)
If this process involves analyzing and designing your system from an object-oriented
point of view, it is called an object-oriented analysis-and-design (OOAD) process.
Then we can use Object Oriented Programming to implement an object-oriented design
as a working system.
Examples of Object-Oriented Programming languages include: C++, Java, C#...
The above figure shows how a Car object can be the template for many other car
instances. Here polo, mini, and beetle are instances.
Daniel K OOP. 4
So, in the above scenario, we will make a new class called Car that will structure a car
object characteristic (attributes) like the cars model, the color, how many passengers it
can hold, its speed etc.
The class can define types of operations or methods that can be performed on a car
object. For e.g., the car class might specify an accelerate method, which would update
the speed attribute of the car object.
A class is a template used to create multiple objects with similar Features. Each object
is associated with the data of type class with which they are created.
For example, mango, apple and orange are members of class Fruit.
The below figure shows representation of an Object.
✓ Encapsulation
✓ Abstraction
✓ Inheritance
✓ Polymorphism
Encapsulation
The wrapping (packaging) up of data and methods into a single unit (called class) is
known as encapsulation.
Data encapsulation is the most remarkable feature of a class.
The data is not accessible to the outside world and only those methods, which are
wrapped in the class, Can access it.
This insulation (protecting) of the data from direct access by the program is called data
hiding.
Daniel K OOP. 5
Abstraction
In inheritance, a new class is defined from a pre-existing class. Which leads to the
situation,
The new class has all the functionality of the pre-existing class and additionally,
introduces its own specific functionality.
The new class inherits the functionality of another existing class.
o While speaking of inheritance, the existing class is called the superclass (base
class) and the new class is called subclass (derived class).
E.g., graduate student and undergraduate student
o They have many data and method member in common
In OOP, the concept of inheritance provides the idea of reusability.
Polymorphism
✓ Polymorphism means the ability to take more than one form.
Daniel K OOP. 6
Dynamic Binding
✓ It refers to the linking of a procedure call to the code to be executed in response to the
call.
✓ Dynamic binding means that the code associated with a given procedure call is not
known until the time of the call at run time
✓ A procedure call associated with a polymorphic reference depends on the dynamic type
of that reference
For example, consider the procedure ‘draw’ in the above figure. By inheritance, every
object will have this procedure. Its algorithm is, however, unique to each object and so the
draw procedure will be redefined in each class that defines the object. At run-time, the
code matching the object under current reference will be called.
Message Communication
An object-oriented program consists of a set of objects that communicate with each
other.
The process of programming in object-oriented language, therefore, involves the
following three basic steps:
o Creating classes that define objects and their behavior.
o Creating objects from class definitions.
o Establishing communication among objects.
Objects communicate with one another by sending and receiving information much the
same way as people pass messages to one another.
A message for an object is a request for execution of a procedure, and therefore will
invoke a method (procedure) in the receiving object that generates the desired result as
shown in Fig.
Daniel K OOP. 7
A message for an object is a request for execution of a procedure, and the information
to be sent.
For example, consider the statement Employee.Salary (name); Here, Employee is the
object, Salary is the message and name is the parameter that contains information.
Benefits of OOP
In OO approach:
By using Inheritance, we can eliminate redundant code and extend the use of the
existing classes.
By the principle of data hiding, we can build secure program that can’t be invaded by
It is easy to partition the work in a project based on object which reduce complexity.
Daniel K OOP. 8