lecture 2

28
Lecture-2 Instructor Name: Object Oriented Programming

Upload: talha-ijaz

Post on 13-Feb-2017

140 views

Category:

Automotive


0 download

TRANSCRIPT

Page 1: Lecture 2

Lecture-2Instructor Name:

Object Oriented Programming

Page 2: Lecture 2

Today’s Lecture

Information Hiding

Encapsulation Interface

Implementation

Abstraction

2

Page 3: Lecture 2

Information Hiding

Introduction Information hiding is one the most important principle of OOP

which says the private information should only be accessible to

the owner.

Information Hiding means “Showing only those details to the

outside world which are necessary for the outside world and

hiding all other details from the outside world.”

Information is stored within the Object

Information within the object is not accessible to outside world

Information can only be manipulated by the object 3

Page 4: Lecture 2

Information Hiding

Introduction Information hiding: a module is characterized by the information

it hides from other modules, which are called its clients. The hidden information remains a secret to the client modules. (Ghezzi et al)

The purpose of Information hiding is to make inaccessible certain details that should not affect other parts of a system.

(Ross et al)

Information hiding is the principle that users of a software component (such as a class) need to know only the essential details of how to initialize and access the component, and do not need to know the details of the implementation. (Budd)

4

Page 5: Lecture 2

Information Hiding

Information Hiding – Real Life Examples A Person’s (Say Ali) name other information is stored in this brain

and we can not access it directly

An Email server has millions of email address but it shares only our account information with us

A phone SIM card may store several phone numbers but we can not read numbers directly rather a phone set (a person’s own) will read this information for us

5

Page 6: Lecture 2

Information Hiding

Information Hiding in OOP’s Perspective In OOP an Object have state and behaviors that is hidden from

outside world. So in OOP perspective information hiding is

“Hiding object detail (state & behavior) from the users”

Term “user” defined here means an “Object” of another class that is calling functions of this class using or it may be another program in which we are using this class.

6

Page 7: Lecture 2

Information Hiding

Advantages of Information Hiding Simplifies Object Oriented Model

In OOP Model we only have object and their interaction

Barrier against change propagation

Any change in the functionality doesn’t effect the OOP model

7

Page 8: Lecture 2

Information Hiding

Achieving Information Hiding Information hiding can be achieved using

1.Encapsulation

2.Abstraction

8

Page 9: Lecture 2

Encapsulation

Introduction

9

Page 10: Lecture 2

Encapsulation

What is Encapsulation? Encapsulation means “All the characteristics of an object are

hidden in the object itself”.

The wrapping up of data and function into a single unit is known as encapsulation.

Encapsulation hides the implementation details

Encapsulation and information hiding are the same concepts so they may be used as synonyms.

10

Page 11: Lecture 2

Encapsulation

Encapsulation and Object Data and Behaviour are tightly coupled inside an object

Information structure and implementation details of its operations are hidden from outside world.

11

Page 12: Lecture 2

Encapsulation

Encapsulation Example A Person (Say Ali) stores his personal information in itself and

its behaviour is also implemented in it. (Ali is an object of person)

Now its up to Object Ali whether he wants to share this information with outside world or not.

Attributes and Behaviour of Ali are encapsulated in itself.12

Page 13: Lecture 2

Encapsulation

Encapsulation Example A Phone stores phone numbers in digital format and knows

how to convert it into human-readable characters We don’t know

How the data is stored How it is converted to human-readable characters

13

Page 14: Lecture 2

Encapsulation

Advantages of Encapsulation Followings are the advantages of Encapsulation

1.Simplicity and Clarity

2.Low Complexity

3.Better Understanding

4.Ensure Structural Changes remain Local

5.Encapsulation Allows adding some logic when accessing client’s data

For Example validation on modifying a property

14

Page 15: Lecture 2

Interface

Object and Interface Every Object has an Interface. Interface is set of functions of an object that he wants to

expose to other objects. An object encapsulates data and behaviour. So how will an

object interact with other objects? Objects communicate through this interface.

15

What would be the interface of a Phone?

Page 16: Lecture 2

Implementation

Object and Implementation The behaviour of object requires implementation. The actual implementation (coding) of the behaviour in any

object oriented language is called implementation. Implementation has two parts

1. Internal Data Structures to hold an object state that will be hidden from us

2. Functionality in the form of member functions to provide required behaviour

Example – Implementation of Gear BoxData Structure– Mechanical structure of gear box

Functionality– Mechanism to change gear

16

Page 17: Lecture 2

Implementation

Object and Implementation What would be the implementation of an Address Book in a

Phone

• Data Structure– SIM card

• Functionality– Read/write circuitry

17

Page 18: Lecture 2

Interface & Implementation

Separation of Interface & Implementation Only Interface of object is shown and implementation detail is

hidden.

Separating implementation detail from interface is very beneficial as Interface becomes independent of implementation detail

Real Life Example of Separation of Interface & Implementation

Driver has a standard interface to drive a car and using that interface he can drive car of any model or any engine type

18

Page 19: Lecture 2

Messages

What is a message in OOP? A message is a request for an object to perform one of its

operations (methods) All Communication between objects is done via messages The number and kind of messages that can be sent to an

object depends upon its interface

The object to which message is addressed (yourBicycle) The name of the method to perform (changeGear) Any parameter needed by the method (lowerGear) 19

Page 20: Lecture 2

Messages

What is a message in OOP?

20

SHOULD WE INCLUDE THIS SLIDE ?

Page 21: Lecture 2

Abstraction

What is Abstraction? Abstraction means ignoring irrelevant features, properties, or

functions and emphasizing the relevant one

…relevant to the given project (with an eye to future reuse in similar projects)

Abstraction = managing complexities 21

Page 22: Lecture 2

Abstraction

What is Abstraction? Abstraction is something we do everyday

Looking at an object , we see those things about it that have meanings to us.

We abstract the properties of the object, and keep only what we need.

Abstraction allows us to represent a complex reality in terms of simplified model

Abstraction highlights the properties of an entity that we are most interested in and hides the other 22

Page 23: Lecture 2

Abstraction

Abstraction Example“Ali is PHD student and teaches BS students”

Here Object Ali has two perspective one is student perspective and other is teacher perspective.

AttributesStudent Perspective Teacher

PerspectiveName Employee IDStudent Roll No DesignationYear of Study SalaryCGPA Age

Age is common attribute in both perspectives23

Page 24: Lecture 2

Abstraction

Abstraction Example“Ali is PHD student and teaches BS students”

Here Object Ali has different behaviors in student perspective and teacher perspective.

BehaviorStudent Perspective Teacher

PerspectiveStudy Deliver LectureGiveExam TakeExamPlaySports DevelopExam

24

Page 25: Lecture 2

Abstraction

Abstraction Example“A cat can be viewed with different perspectives”

Here Object Cat has different perspective on Ordinary perspective and Surgeon perspective.

Ordinary Perspective Surgeon’s Perspective

A pet animal with A being withFour Legs A SkeletonA Tail HeartTwo Ears KidneySharp Teeth Stomach

25

Page 26: Lecture 2

Abstraction

Abstraction Example

Driver’s View Engineer’s View

26

Page 27: Lecture 2

Abstraction

Abstraction Advantage Abstraction has following advantages

Simplifies the model by hiding irrelevant details Abstraction provides the freedom to defer

implementation decisions by avoiding commitment to details.

27

Page 28: Lecture 2

28