Docsity

Benefits and Principles of Object-Oriented Programming (OOP), Exercises of Compilers

Partial preview of the text

Download Benefits and Principles of Object-Oriented Programming (OOP) and more Exercises Compilers in PDF only on Docsity!

Object-Oriented

Object-Oriented

Programming (OOP)

Programming (OOP)

Lecture No. 2

Lecture No. 2

Benefits of OOP

►

The procedural-oriented languages focus on

procedures, with function as the basic unit.

You need to first figure out all the functions

and then think about how to represent data.

►

The object-oriented languages focus on

components that the user perceives, with

objects as the basic unit. You figure out all

the objects by putting all the data and

operations that describe the user's

interaction with the data.

►

Reusable software

Reusable software : you don't need to

: you don't need to

keep re-inventing the wheels and re-

keep re-inventing the wheels and re-

write the same functions for different

write the same functions for different

situations. The fastest and safest way

situations. The fastest and safest way

of developing a new application is to

of developing a new application is to

reuse existing codes - fully tested and

reuse existing codes - fully tested and

proven codes.

proven codes.

Information Hiding

Information Hiding

►

Information is stored within the object

Information is stored within the object

►

It is hidden from the outside world

It is hidden from the outside world

►

It can only be manipulated by the

It can only be manipulated by the

object itself

object itself

Example – Information Hiding

Example – Information Hiding

►

A phone stores several phone numbers

A phone stores several phone numbers

►

We can’t read the numbers directly

We can’t read the numbers directly

from the SIM card

from the SIM card

►

Rather phone-set reads this

Rather phone-set reads this

information for us

information for us

Information Hiding

Information Hiding

Advantages

Advantages

►

Simplifies the model by hiding

Simplifies the model by hiding

implementation details

implementation details

►

It is a barrier against change

It is a barrier against change

propagation

propagation

Example – Encapsulation

Example – Encapsulation

►

Ali stores his personal information and

Ali stores his personal information and

knows how to translate it to the

knows how to translate it to the

desired language

desired language

►

We don’t know

We don’t know

How the data is stored

How the data is stored

How Ali translates this information

How Ali translates this information

Example – Encapsulation

Example – Encapsulation

►

A Phone stores phone numbers in digital

A Phone stores phone numbers in digital

format and knows how to convert it into

format and knows how to convert it into

human-readable characters

human-readable characters

►

We don’t know

We don’t know

How the data is stored

How the data is stored

How it is converted to human-readable

How it is converted to human-readable

characters

characters

Object has an Interface

Object has an Interface

►

An object encapsulates data and

An object encapsulates data and

behaviour

behaviour

►

So how objects interact with each other?

So how objects interact with each other?

►

Each object provides an interface

Each object provides an interface

(operations)

(operations)

►

Other objects communicate through this

Other objects communicate through this

interface

interface

Example – Interface of a Car

Example – Interface of a Car

►

Steer Wheels

Steer Wheels

►

Accelerate

Accelerate

►

Change Gear

Change Gear

►

Apply Brakes

Apply Brakes

►

Turn Lights On/Off

Turn Lights On/Off

Implementation

Implementation

►

Provides services offered by the object

Provides services offered by the object

interface

interface

►

This includes

This includes

Data structures to hold object state

Data structures to hold object state

Functionality that provides required

Functionality that provides required

services

services

Example – Implementation of

Example – Implementation of

Gear Box

Gear Box

►

Data Structure

Data Structure

Mechanical structure of gear box

Mechanical structure of gear box

►

Functionality

Functionality

Mechanism to change gear

Mechanism to change gear

Separation of Interface &

Separation of Interface &

Implementation

Implementation

►

Means change in implementation does

Means change in implementation does

not effect object interface

not effect object interface

►

This is achieved via principles of

This is achieved via principles of

information hiding and encapsulation

information hiding and encapsulation

Example – Separation of

Example – Separation of

Interface & Implementation

Interface & Implementation

►

A driver can drive a car independent of

A driver can drive a car independent of

engine type (petrol, diesel)

engine type (petrol, diesel)

►

Because interface does not change

Because interface does not change

with the implementation

with the implementation