You are on page 1of 34

Chapter 1

Introduction to
Object Oriented
Programming

By Megha V Gupta, NHITM


Introduction
Object oriented programming is the principle of
design and development of programs using
modular approach.
◦ Object oriented programming approach provides
advantages in creation and development of software for
real life application.
◦ The basic element of object oriented programming is the
data.
◦ The programs are built by combining data and functions
that operate on the data.
◦ Some of the OOP’s languages are C++, Java, C #,
Smalltalk, Perl, and Python.
2
Procedural programming
The procedural programming focuses on
processing of instructions in order to perform a
desired computation.
The top-down concepts to decompose main
functions into lower level components for modular
coding purpose.
Therefore it emphasizes more on doing things like
algorithms.
This technique is used in a conventional
programming language such as C and Pascal.

3
Object oriented programming
Object oriented programming (OOP) is a concept
that combines both the data and the functions that
operate on that data into a single unit called the
object.
An object is a collection of set of data known as
member data and the functions that operate on these
data known as member function.
OOP follows bottom-up design technique.
Class is the major concept that plays important role
in this approach. Class is a template that represents a
group of objects which share common properties
and relationships. 4
Differences
Procedural Programming Object Oriented Programming
Large programs are divided into Programs are divided into
smaller programs known as objects
functions

Data is not hidden and can be Data is hidden and cannot be


accessed by external functions accessed by external functions

Follow top down approach in the Follows bottom-up approach in


program design the program design

Data may communicate with Objects may communicate with


each other through functions each other through functions.
Emphasize is on procedure rather Emphasize is on data rather than
than data procedure

5
History of Java
■ Java was originally designed for interactive television, but it was too advanced
technology for the digital cable television industry at the time.
■ Java team members (also known as Green Team), initiated this project to
develop a language for digital devices such as set-top boxes, televisions, etc.
■ However, it was suited for internet programming. Later, Java technology was
incorporated by Netscape.
■ The principles for creating Java programming were "Simple, Robust, Portable,
Platform-independent, Secured, High Performance, Multithreaded, Architecture
Neutral, Object-Oriented, Interpreted, and Dynamic".
■ Java was developed by James Gosling, who is known as the father of Java, in
1995. James Gosling and his team members started the project in the early
'90s.
■ James Gosling - founder of java
■ Currently, Java is used in internet programming, mobile devices, games, e-
business solutions, etc. There are given significant points that describe the
history of Java.
■ Java is an island of Indonesia where the first coffee was produced
(called java coffee). It is a kind of espresso bean. Java name was
chosen by James Gosling while having coffee near his office.
■ Notice that Java is just a name, not an acronym.
Features of Java
Platform Independent and
secured
OOP
■ Object-Oriented Programming is a methodology
or paradigm to design a program using classes and
objects. It simplifies software development and
maintenance by providing some concepts:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
OOP Concepts

1
5
Objects
Objects are basic building blocks for designing
programs.
An object is a collection of data members and
associated member functions.
An object may represent a person, place or a table
of data.
Each object is identified by
a unique name. Each object must
be a member of a particular class.
Example: Apple, orange, mango
are the objects of class fruit.
1
6
Objects
■ Any entity that has state and behavior is known as
an object. For example, a chair, pen, table,
keyboard, bike, etc. It can be physical or logical.
■ An Object can be defined as an instance of a class.
An object contains an address and takes up some
space in memory.
■ Example: A dog is an object because it has states
like color, name, breed, etc. as well as behaviors
like wagging the tail, barking, eating, etc.
q
Encapsulation

Data Encapsulation:
◦ The wrapping of data and functions into a single unit
(class) is called data encapsulation.
◦ Data encapsulation enables data hiding and information
hiding.
Data hiding:
It is a method used in object oriented programming to
hide information within computer code.
Abstraction
Abstraction refers to the quality of dealing with ideas
rather than events. It basically deals with hiding the
details and showing the essential things to the user. If
you look at the image here, whenever we get a call,
we get an option to either pick it up or just reject it.
But in reality, there is a lot of code that runs in the
background. So you don’t know the internal
processing of how a call is generated, that’s the
beauty of abstraction. Therefore, abstraction helps to
reduce complexity. You can achieve abstraction in
two ways:
a) Abstract Class
b) Interface
Encapsulation
■ The best way to understand encapsulation is to look at the
example of a medical capsule, where the drug is always
safe inside the capsule. Similarly, through encapsulation
the methods and variables of a class are well hidden and
safe.
10
Principles of OOPs

▸Polymorphism
If one task is performed by different ways, it
is known as polymorphism.

For example: To convince the customer


differently, to draw something, like shape,
triangle, rectangle , a cat speaks meow, dog
barks woof, etc.
- Polymorphism present a method that
can have many definitions.
Polymorphism is related to
- Overloading → Compile time
polymorphism/run time polymorphism
- Overriding → Run time polymorphism/
Dynamic polymorphism
- Syntax
getPrice()
getPrice(string name)
Polymorphism
■ Polymorphism means taking many forms, where ‘poly’ means
many and ‘morph’ means forms. It is the ability of a variable,
function or object to take on multiple forms. In other words,
polymorphism allows you define one interface or method and
have multiple implementations.
■ Let’s understand this by taking a real-life example and how this
concept fits into Object oriented programming.
Polymorphism
The ability of an operator and function to take
multiple forms is known as Polymorphism.
Polymorphism in Java is of two types:
1.Compile time Polymorphism (Method Overloading)
2. Run time Polymorphism (Method Overriding)
Overloading
Overloading allows objects to have different
meaning depending upon context.
There are two types of overloading viz.
o Operator Overloading
o Function Overloading
When an existing operator operates on new data
type is called operator overloading.
Function overloading means two or more
function have same name, but differ in the
number of arguments or data type of arguments.
Inheritance
Inheritance is the process by which one
object can acquire and use the properties of
another object.
The existing class is known as base class or super
class.
The new class is known as derived class or sub class.
The derived class shares some of the properties of the
base class. Therefore a code from a base class can be
reused by a derived class.
What Is Inheritance?

Inheritance provides a powerful and natural mechanism for


organizing and structuring your software. Now we will
explain how classes inherit state and behavior from their
super classes, and explains how to derive one class from
another using the simple syntax provided by the Java
programming language.
Inheritance example
Dynamic binding & Message
Passing
Dynamic binding:
◦ Binding is the process of connecting one program to another.
◦ Dynamic binding is the process of linking the procedure call
to a specific sequence of code or function at run time or
during the execution of the program.
Message Passing:
◦ In OOP’s, processing is done by sending message to objects.
◦ A message for an object is request for execution of
procedure.
◦ Message passing involves specifying the name of the object,
the name of the function (message) and the information to be
sent.

You might also like