41
Object Oriented Programming F3031 INTRODUCTION TO OOP

Object Oriented Programming F3031

Embed Size (px)

DESCRIPTION

Object Oriented Programming F3031. INTRODUCTION TO OOP. INTRODUCTION TO OOP. Objectives: Know the concept of OOP Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know four design principles of OOP - PowerPoint PPT Presentation

Citation preview

Page 1: Object Oriented Programming F3031

Object Oriented ProgrammingF3031

INTRODUCTION TO OOP

Page 2: Object Oriented Programming F3031

INTRODUCTION TO OOP

Objectives: Know the concept of OOP Know the difference between functional

programming and OOP Know basic terminology in OOP Know the importance of OOP Know four design principles of OOP Know OOP programming languages

Page 3: Object Oriented Programming F3031

INTRODUCTION TO OOP

Object oriented programming is introduced as a new programming concept which should help one in developing high quality software.

It attempts to solve the problem with only one approach by dividing the problems in sub-modules and using different objects

Objects of the program interact by sending messages to each other.

Page 4: Object Oriented Programming F3031

INTRODUCTION TO OOP

Programming with objects is quite like working with real world objects.

It groups operations and data into modular units called objects.

These objects can be combined into structured networks to form a complete program, similar to how the pieces in a puzzle fit together to create a picture.

Page 5: Object Oriented Programming F3031

INTRODUCTION TO OOP

By breaking down complex software projects into small, self-contained and modular units, object orientation ensure that changes to one part of software project will not affect other portions of the software.

Page 6: Object Oriented Programming F3031

STRUCTURED vs. OO PROGRAMMING

STRUCTURED PROGRAMMING:

MAIN PROGRAM

FUNCTION 3FUNCTION 2

GLOBAL DATA

FUNCTION 5FUNCTION 4

FUNCTION 1

Page 7: Object Oriented Programming F3031

Structured Programming

There are features of structured programming:

Emphasis is on doing things (algorithms) Using function Function & program is divided into modules Every module has its own data and function

which can be called by other modules. Most of the functions share global data

Page 8: Object Oriented Programming F3031

Structured Programming(2)

Data move openly around the system from function to function

Functions transform data from one form to another

Employs top down approach in program design.

Page 9: Object Oriented Programming F3031

OBJECT ORIENTED PROGRAMMING

Object 1 Object 2

Data

Function

Data

Function

Object 3

Data

Function

Page 10: Object Oriented Programming F3031

OBJECT ORIENTED PROGRAMMING

There are features of object oriented programming:

Emphasis is on data rather than procedure Programs are divided into what are known

as objects Data structures are designed such that they

characterize the objects. Functions that operate on the data of an

object are tied together in the data structure

Page 11: Object Oriented Programming F3031

OBJECT ORIENTED PROGRAMMING(2)

Data is hidden and cannot be accessed by external functions.

Objects may communicate with each other through functions.

New data and functions can be easily added whenever necessary

Follows bottom-up approach in program design.

Page 12: Object Oriented Programming F3031

THE DIFFERENCES BETWEEN FUNCTIONAL PROGRAMMING AND

OOPFUNCTIONAL PROGRAMMING OOP

Emphasis is on doing things (algorithms)

Emphasis is on data rather than procedure

Data and functions are kept separately. Data and functions are group in class

Design is not very strong, hard to understand and difficult to implement.

Design of the whole system could be understand by others (even doesn’t have background on science computer)

Using top-down approach. It breaks a program down into components until they cannot be composed anymore.

Using bottom-up approach in program design

Page 13: Object Oriented Programming F3031

Why OO is chose?

Current problem of software:• Software is difficult to develop, maintain and modify.• Most software is over budget and delivered late.• Programmers still have to create software from the

ground-up.

OO introduces techniques that help:• Developing a more cost-effective and efficient software

that will be delivered on time.• Adapt quickly to new changes or client demand.

Page 14: Object Oriented Programming F3031

Object-Oriented Programming Languages

Pure OO LanguagesSimula• Simulation programming language• Found in the late 1960’s • By O.J. Dahl and Kristen NygaardSmalltalk• Introduced in the early 1970’s• By Alan Kay• First successful object oriented language.

Page 15: Object Oriented Programming F3031

Object-Oriented Programming Languages

Hybrid OO LanguagesC++, Eiffel, etc• Emerged in the mid 1980’s

Page 16: Object Oriented Programming F3031

The advantages of using OOP.

OOP offers several advantages to both program designer and the user. Object orientation contributes to the solution of many problems associated with the development and quality software and lesser maintenance cost. The principles advantages are :

i. Through inheritance, we can eliminate redundant code and extend the use of existing class

ii. We can build programs from the standard working modules that communicate width one another, rather than having to start writing the code from scratch. This leads to saving development time and higher productivity.

Page 17: Object Oriented Programming F3031

iii. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of program.

iv. It is easy to partition the work in a project based on objects.

v. Object oriented system can be easily upgraded from small to large systems.

Page 18: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING

i. Object

ii. Class

iii. Message–Object Communication

Page 19: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(2)

The key concepts are:• Object

-Directly relate to the real world entities.-Can be a person, thing or concept (a noun).-Like a “black box”, therefore all the

implementation is hidden.

Page 20: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(3)–Object has a• Attribute - description of objects in a

class•Method - an action performed by an

object (a verb)• Identity (unique name)

Page 21: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(4)

Example for attributes and methodsAttributes:–manufacturer’s name–model name–year made– color–number of doors– size of engine– etc.

Page 22: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(5)

Methods:–Define data items (specify manufacturer’s

name, model, year, etc.)–Change a data item (color, engine, etc.)–Display data items–Calculate cost– etc.

Page 23: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING (6)

• Class– A generic definition for a set of similar

objects.– Provides the specifications for the

objects’ behaviors and attributes.– An abstraction of a real world entity.

Page 24: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(7)

Object vs Class• object is created from a class. • object is considered as an instance of a class. • class is considered as a template from which

objects are instantiated • can create an object or many objects from a

class.

Page 25: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(8)

Diagram 1: Class Car

Car

DoorSeatTypeModel

DriveStopLockUnlock

Diagram 2: MyCar as an ObjectObject vs Class

Page 26: Object Oriented Programming F3031

BASIC CONCEPT OF OBJECT ORIENTED PROGRAMMING(9)

MessagesRequests for the receiver objects to carry out the indicated method or behavior and return the result of that action to the sender objects

Page 27: Object Oriented Programming F3031

BASIC TERMINOLOGY OF OBJECT ORIENTED PROGRAMMING(10)

Object CommunicationsObjects communicate by sending messages

Page 28: Object Oriented Programming F3031
Page 29: Object Oriented Programming F3031
Page 30: Object Oriented Programming F3031

Design Principles of OOP

Four main design principles of Object-Oriented Programming(OOP):

Abstraction Encapsulation Polymorphism Inheritance

Page 31: Object Oriented Programming F3031

Abstraction

Data abstraction is a process to delete all unnecessary attributes and remain the necessary attributes to describe an object.

Object in a program is an abstraction from a real object (in real world).

Attributes characteristics, which can be seen. Behaviours actions that are done to an object. Figure 1.1 shows how data abstraction is done for

class Student and Figure 1.2 for class Box.

Page 32: Object Oriented Programming F3031

Abstraction(2)

CHARACTERISTIC/ATRRIBUTES

BEHAVIOUR/METHOD

ABSTRACTION

OBJECT STUDENT

NAME, MATRIK NUMBER, ADDRESS, IC NUMBER

CALCULATE_MARK (),DETERMINE_GRED(),PRINT_RESULT()

FIGURE 1.1

CLASS STUDENT

Page 33: Object Oriented Programming F3031

Abstraction(3)

OBJECT BOX

ABSTRACTION

CHARACTERISTIC/ATRRIBUTES

BEHAVIOUR/METHOD

OBJECT BOX

Length, width, depth

Calculate_Volume()Calculate_Area()

CLASS BOX

FIGURE 1.2

Page 34: Object Oriented Programming F3031

Abstraction(3)

Focus only on the important facts about the problem at hand

To design, produce and describe so that it can be easily used without knowing the details of how it works.

Analogy:• When you drive a car, you don’t have to know how

the gasoline and air are mixed and ignited.• Instead you only have to know how to use the

controls.• Draw map

Page 35: Object Oriented Programming F3031

Encapsulation

• Encapsulation is a process of tying together all data and methods that form a class and control the access to data by hiding its information.

• It enables access to object just by using methods of that object.

• It is one of the security features in object-oriented programming (OOP).

• Figure 1.3 shows the concept of encapsulation for a class Student and Figure 1.4 for class Box.

Page 36: Object Oriented Programming F3031

Encapsulation(2)

Student

private : int matricNumint icNum char name[30]char address[100]

public :double calculate_mark() void determine_mark()void print_result()

Box

private : float lengthfloat width float depth

public :float calculate_volume() float area()

FIGURE 1.3 FIGURE 1.4

Page 37: Object Oriented Programming F3031

Encapsulation(3) Also known as data hiding Only object’s methods can modify information in

the object. Process of hiding the implementation details of an

object. Access to manipulate the object data is through its

interface (operations/ functions). Protects an object’s internal state from being

corrupted by other programs.

Page 38: Object Oriented Programming F3031

Encapsulation(4)

Program maintenance is easier and less expensive because changes in the object data or implementation is only modified in one place

Allows objects to be viewed as black boxes.

Page 39: Object Oriented Programming F3031

Polymorphism

the same word or phrase can mean different things in different contexts

Analogy: In English, bank can mean side of a river or a place to put money

move -

Page 40: Object Oriented Programming F3031

Inheritance

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. Superclass – inherit its attributes & methods to the

subclass(es). Subclass – can inherit all its superclass attributes &

methods besides having its own unique attributes & methods.

Page 41: Object Oriented Programming F3031

An Inheritance Hierarchy

41

Vehicle

Automobile Motorcycle Bus

Sedan Sports Car School BusLuxury Bus

What properties does each vehicle inherit from the types of vehicles above it in the diagram?

Superclass

Subclasses