0% found this document useful (0 votes)
16 views

7 Object Oriented Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

7 Object Oriented Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

OOPs

• Object oriented programming as an approach that provides a way of modularizing


programs by creating partitioned memory area for both data and functions that can
be used as templates for creating copies of such modules on demand.
• Emphasis is on doing rather than procedure.
• Programs are divided into what are known as objects.
• Data is hidden and can’t be accessed by external functions.
• Objects may communicate with each other through functions.
• New data and functions can be easily added.
• Follows bottom-up approach in program design.
OOPs
• In OOP programmers define not only the data type of a data
structure, but also the types of operations/methods (functions)
that can be applied to the data structure.
• In this way, the data structure becomes an object that includes
both data and functions (methods) in one unit.
• In addition, programmers can create relationships between one
object and another.
• For example, objects can inherit characteristics from other
objects.
OOP Basic Terminology
Object
- usually a person, place or thing (a noun)
Method
- an action performed by an object (a verb)
Property or attribute
- Characteristics of certain object.
Class
- a category of similar objects (such as automobiles),
does not hold any values of the object ’s
attributes/properties
Basic concepts of OOP
• Objects
• Classes
• Data abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Objects
• The fundamental idea behind object oriented approach is to combine both
data and function into a single unit and these units are called objects.
• An Object is an identifiable entity with some characteristics and behaviour.
• An Object is an instance of a Class.
More formal definition of an “Object”

An object is a computational entity


that:
1. Encapsulates some state
2. Is able to perform actions, or
methods, on this state
3. Communicates with other objects
via
message passing
Class
• Collection of objects is called class.
• It is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class.
A class is like a blueprint for an object.
• For Example: Consider the Class of Cars. There may be many cars with
different names and brand but all of them will share some common properties
like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here,
Car is the class and wheels, speed limits, mileage are their properties.
Syntax
Example
Class/
Object

Each copy of an
from a
objec
particul class is
t
ar
called an instance
of the class.
Class/
Object

The act of
creating a new
instance of an
class is called
instantiation.
Technical contrast between
Objects & Classes

CLASS OBJECT
Class is a data type Object is an instance of Class.

It generates OBJECTS It gives life to CLASS

Does not occupy memory It occupies memory location.


location
It cannot be manipulated It can be manipulated.
because it is not available
in memory (except static class)
Abstraction
• Abstraction refers to the act of representing essential features without
including the back ground details or explanations i.e., to represent the needed
information in program without presenting the details.
• Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of the car or applying brakes
will stop the car but he does not know about how on pressing accelerator the
speed is actually increasing, he does not know about the inner mechanism of
the car or the implementation of accelerator, brakes etc in the car.
Type of
Classes

Concrete Abstract
Class Class

Can be instantiated Can’t be instantiated


directly directly
Abstracti
on
• Abstraction is a design principle.

• Is the process of removing characteristics from


something in order to reduce it to a set of essential
characteristics.
• but
Through
the relevantdata
the processof about
abstraction,
a classa in to
programmer hides all reduce
order complexity and increase reusability.
• Abstraction is a basic representation of a
concept.
Abstracti
on
• Abstraction allows programmers to represent
complex real world in the simplest manner.
• It is a process of identifying the relevant qualities
and behaviors an object should possess, in other
word represent the necessary features without
representing the back ground details
• You should always use abstraction to ease
reusability, and understanding for the design and
enable extension.
• When we design the abstract classes, we
define the
Abstracti
on
• An abstract class, which declared
with the
“abstract” keyword, cannot be
instantiated.
• It can only be used as a super-class
for other classes that extend the
abstract class. Abstract class is a
design concept and implementation
gets completed when it is being
Abstraction - type of classes
DOB:
Person Name:
Address
:

Concrete Abstract
Class Class

Can be instantiated Can’t be instantiated


directly directly

DOB DOB:
: Teache Stude Name:
Name r nt Address
: Address: : GPA:
Specializatio Courses
n: Academic : Etc…:
Title:
Etc…:
Can be instantiated
Abstracti
on
• An abstract class is a class that may not have any
direct instances.
• An abstract operation is an that it
operation incomplete and
requires
implementation aof the operation.
child to is
supply
Shape
{abstract} Abstract class
draw () {abstract} an
Abstract operation

Circle Rectangle

draw () draw ()
Encapsulation
Encapsulation
• Encapsulation is defined as wrapping up of data and information under a single unit. In Object-Oriented
Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
• The data is not accessible to the outside world and only those functions which are wrapped in the class can
access it.
• Consider a real-life example of encapsulation, in a company, there are different sections like the accounts
section, finance section, sales section etc. The finance section handles all the financial transactions and
keeps records of all the data related to finance. Similarly, the sales section handles all the sales-related
activities and keeps records of all the sales. Now there may arise a situation when for some reason an
official from the finance section needs all the data about sales in a particular month. In this case, he is not
allowed to directly access the data of the sales section. He will first have to contact some other officer in the
sales section and then request him to give the particular data. This is what encapsulation is. Here the data of
the sales section and the employees that can manipulate them are wrapped under a single name “sales
section”.
Encapsulati
on
• Is the inclusion of property & method within
a class/object in which it needs to function
properly.
• Also, enables reusability of an instant of an
already implemented class within a new
class while hiding & protecting the method
and properties from the client classes.
Encapsulati
on
• The class is kind of a container or capsule or
a cell, which encapsulate the set of methods,
attributes and properties to provide its indented
functionalities to other classes.
• In that sense, encapsulation also allows a class to
change its internal implementation without
hurting the overall functioning of the system.
• That idea of encapsulation is to hide how a class
does its operations while allowing requesting its
operations.
Encapsulation –
Benefits
⬥ Ensures that structural changes remain local:
⬩ Changing the class internals does not
affect anycode outside of the class
⬩ Changing methods'
implementation does not reflect the clients using
them
⬥ Encapsulation allows adding some
logic when accessing client's data
⬩ E.g. validation on modifying a property value

⬥ Hiding implementation details reduces


complexity
4
Inheritance
• The capability of a class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
• Sub Class: The class that inherits properties from another class is called Sub class or Derived
Class.
• Super Class: The class whose properties are inherited by sub class is called Base Class or
Super class.
• Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a
new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class. By doing this, we are reusing the fields and
methods of the existing class.
Inheritan
ce
• Inheritance—a way of organizing classes
• Term comes from inheritance of traits like
eye color, hair color, and so on.
• Classes with properties in common can be
grouped so that their common properties
are only defined once in parent class.
• Superclass – inherit its attributes &
methods to the subclass(es).
• Subclass – can inherit all its superclass
attributes & methods besides having its
own unique attributes & methods.
Inheritan
ce
• Inheritance allows child classes to inherit
the characteristics of existing parent class
• Attributes (fields and properties)
• Operations (methods)
• Child class can extend the parent class
• Add new fields and methods
• Redefine methods (modify existing behavior)
• A class can implement an interface by
providing implementation for all its
methods
Inheritan
ce
• Expresses commonality
among classes/objects
• Allows code reusability
• Highlights relationships
• Helps in code organization
Inheritan
ce
Inheritance –
Example
Base
Person class
+Name: String
+Address:
String
Derived Derived
class class

Employee Student
+Company: +School: String
String
+Salary: double
+Name: String +Name: String
+Address: +Address:
String String
An Inheritance
Hierarchy
Superclas
Vehicl s
e
Subclasse
s

Automobi Motorcycl Bu
le e s

Seda Sports School Luxury


n Car Bus Bus
Example: Single
Inheritance
One class inherits from
another.
Account
Ancestor
- balance
Supercl - na m e
- number
ass
(parent) + withdraw()
+
createStatement()
Inheritanc
e
Relations
hip

Savings Checking
Subclasse
s
Descenden
Example: Multiple
Inheritance
• A class can inherit from several
other classes.
FlyingThin Animal
g
Multiple
Inheritance

Airplane Helicopte Bird Wolf Horse


r

Most modern languages don’t support multiple


inheritance!
Polymorphism
• The word polymorphism means having many forms.
• An operation may exhibit different behaviours in different instances. The
behaviour depends upon the types of data used in the operation.
• C++ supports operator overloading and function overloading.
 Operator Overloading: The process of making an operator to exhibit
different behaviours in different instances is known as operator overloading.
 Function Overloading: Function overloading is using a single function name
to perform different types of tasks.
Example of polymorphism
Dynamic binding

• Binding refers to the linking of a procedure call to the code to the executed
in response to the call.
• Dynamic binding means the code associated with a given procedure call is
not known until the time of the call at run-time
• In dynamic binding, the code to be executed in response to function call is
decided at runtime.
Message passing

• Objects communicate with one another by sending and receiving


information to each other.
• A message for an object is a request for execution of a procedure and
therefore will invoke a function (procedure) in the receiving object that
generates the desired result.
• Message passing involves specifying the name of the object, the name of
the function (message) and information to be sent.
Advantages of
OOP
• Code reuse & recycling
• Improved software-development productivity
• Improved software maintainability
• Faster development
• Lower cost of development
• Higher-quality software
• Encapsulation
Disadvantages of
OOP
• Steep learning curve
• Could lead to larger program
sizes
• Could produce slower
programs
OOP
Suitability
• Object oriented programming is
good in complex projects or
modular type of systems. It allows
simultaneous system development
teams and also could aid in agile
system development
environments like Xtreme
Programming.

You might also like