7 Object Oriented Programming
7 Object Oriented Programming
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.
Concrete Abstract
Class Class
Concrete Abstract
Class Class
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
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
Savings Checking
Subclasse
s
Descenden
Example: Multiple
Inheritance
• A class can inherit from several
other classes.
FlyingThin Animal
g
Multiple
Inheritance
• 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