33
DCS 5085 DCS 5085 Object Oriented Object Oriented Programming Programming Chapter 1 Chapter 1

DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

Embed Size (px)

Citation preview

Page 1: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

DCS 5085DCS 5085

Object Oriented Object Oriented ProgrammingProgramming

Chapter 1Chapter 1

Page 2: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

OverviewOverview

• Introduction to Programming

• Programming Methodology

• Comparison of Programming Paradigms

• Basic Concept of Object Oriented Programming

• Procedural vs. Object Oriented language

Page 3: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

1.1 Introduction to Programming

What is a..

- programming Language?A programming language is a standardized communication technique for expressing instructions to a computer

- software program?A computer programme is a piece of software that has a specific task, such a clock or calculator, or which is a tool for the user, such as a Word-processor.

Page 4: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

More what is...

What is..- Syntax? (sin-tax)Set of rules that denote how to express instructions to computer. Grammar of the programming language.

- Debug? (dee-bug)To fix errors in the program.

- Compile? (kom-pile)To convert the code into machine language. Need to use a compiler. Different compilers work differently.

Page 5: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

1.2 Programming Paradigms

• Traditional programming methodology

Algorithms + Data Structures = Programs Niklaus Wirth

(recipe + ingredients = cake)

• Programming Paradigm is view that the programmer has of the execution of the program: for instance, in the case of object-oriented programming, the programmer sees the execution of the program as a collection of dialoguing objects.

• Programming Paradigms1. Unstructured2. Procedural3. Structured/Modular4. Object-Orientated

Focus : functionality

Focus : data + functionality

Page 6: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Usually done when people start learning programming by writing small and simple programs.

• Program written with minimal organisation and planning

• I.e. program consisting of one main program.– “main program” stands for a sequence of commands or

statements which modify data which is global throughout the whole program.

• Advantage

– Easy and efficient (if program small)

• Disadvantages:

– Messy and inefficient (When program large)

• Easy to make mistakes

• Cannot re-use code – have to be rewritten (thus modular/OOP paradigm)

1.2.1 Unstructred Programming

Page 7: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• The main program is responsible to pass data to the individual calls, the data is processed by the procedures and, once the program has finished, the resulting data is presented.

• Able to combine returning sequences of statements into one single place.

• A procedure call is used to invoke the procedure.

• After the sequence is processed, flow of control proceeds right after the position where the call was made

• By introducing parameters as well as procedures of procedures (subprocedures) programs can now be written more structured and error free.

1.2.2 Procedural Programming

Main program

Procedure 1 Procedure 2 Procedure 3

Procedure calls

Page 8: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Advantages:

– Programs can be written in more structured and error free.

• Disadvantages:

– Main program has to hold all the data. No data encapsulation.

Page 9: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Also know as Structural programming

• Procedures of a common functionality are grouped together into separate modules.

• Each module can interact through procedure calls.

• Each module can have its own data. This allows each module to manage an internal state which is modified by calls to procedures of this module.

• However, there is only one state per module and each module exists at most once in the whole program.

1.2.3 Modular Programming

Page 10: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Disadvantages:

– No data protection

Any external functions can access the members of module.

– Explicit creation and destruction

Integer must declared within a particular scope. Once you leave the scope the integer is lost. This is automatically created and destroyed.

Page 11: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• In an object oriented environment:– Software is a collection of discrete objects with their data

and functionality to model “Real World” objects.– All objects can be easily replaced, modified, and reused.– Everything is an object and object is responsible for itself.– Emphasizes its cooperative philosophy by allocating

tasks among the objects of the applications.– All software problems shell focused on “What they do”

rather than “How they do it”.

1.2.4 Object Orientated Programming

Page 12: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

Room

Table

Chair

Student

Lecturer

Description/DataColor : redType : plasticCost : RM 45PurchaseDate : 25thMay2003

Functionality/MethodsArrange ()Stack_up ()Break ()

Page 13: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

1.3 Overall Comparison

Page 14: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

1)Objects

2)Classes

3)Data Abstraction and Encapsulation

4)Inheritance

5)Polymorphism

6)Dynamic binding

7)Message communication

1.4 Basic Concepts of OOP

Page 15: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Each object contains data (variables) and code (methods) to manipulate data.

• Objects interact by sending messages to one another.

• Objects can interact without having to know details of each other’s data or code.

• It is sufficient to know the type of message accepted and the type of response returned by the objects.

• Example:

– Objects: Customer and Account

– Customer object may send a message to the account object requesting for the bank balance.

1.4.1 Objects

Page 16: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Real world object:– Two characteristics: State and Behavior – Behavior:

• I.e. response to message – State:

• I.e. height and sex• Software object:

– Two characteristics: State and Behavior– Behavior: What it can do

• I.e. methods– State: what it contains.

• I.e. variables• Objects can be defined as software bundles of data and

related methods.

Page 17: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Since everything that an object can do is expressed through its methods, message passing supports all possible interactions between objects.

• To send and receive messages back and forth, objects don't need to be in the same process or even on the same machine.

• An object oriented programmer looks at a problem as a system of interacting objects

Message Passing

Object 1 Object 2

Page 18: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• A class is a blueprint for an object.

• A class does not represent an object.

• A class represents all the information a typical object should have as well as all the methods it should have.

• In other words, a class is an abstraction for a bundle of data and operations.

• Once a class has been defined, we can create any number of objects belonging to that class.

• e.g. Cookie = Object, Class = Cookie Cutter. Use Cookie Cutter to make objects.

1.4.2 Classes

Page 19: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• When you create an object of a class (create an instance of a class) it gets its own copy of state initialized with certain values.

• Each of instance of a class is a separate object and is treated differently, but will have similar behaviors.

• e.g.

Class :: Person---------------------variables :: name: age: gendermethods :: eat: sleep: playCS

Person :: Azrul--------------------------name : Azrul Coolage : 18gender : maleeat ()sleep()playCS()

Person :: Jenny--------------------------name : Jenniferage : 16gender : femaleeat ()sleep()playCS()

Page 20: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Following is an example of a class and its instance.

VariablesHeightWeightSex name

MethodsM1(…)M2(…)M3(…)

Height = 165Weight = 70Sex = maleName = Tom

M1(…)M2(…)M3(…)

class Person Person p

Objects & Classes

Page 21: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Packaging an object’s variables within the protective custody of its methods is call encapsulation.

• Encapsulation is used to hide unimportant implementation details from other objects.

• Abstraction refers to the act of representing essential features without including the background details or explanations.

• Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and functions to operate on these attributes.

• Since the classes use the concept of data abstraction, they are known as Abstract Data Types (ADT).

1.4.4 Data Abstraction & Encapsulation

Page 22: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Benefit of encapsulation:

– Modularity – the source code for an object can be written and maintained independently of the source code for other objects.

– Information hiding – object can maintain private information and methods that it can change at any time without affecting the other objects that depend on it.

Methods

Variables

An object's variables make up the center or nucleus of the object and the methods surround and usually hide the object's nucleus from other objects in the program.

Page 23: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Inheritance is the process by which objects of one class acquire the properties of objects of another class.

• The base class (super class) represents the generalized model of objects you are dealing with.

• The derived classes (sub classes) represent specialized objects constructed by customizing the base class for a specific purpose.

• Each derived class shares common characteristics with the class from which it is derived.

– I.e. cats and dogs are subclasses of a superclass called animals

• The concept of inheritance provides the idea of reusability.

• Changes in Superclass will effect Subclass

1.4.5 Inheritance

Page 24: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

Class :: Person---------------------variables :: name: age: gendermethods :: eat: sleep: playCS

Class :: Sniper---------------------variables :: accuracy: experiencemethods :: shoot: hide: snipe

Class :: Rusher---------------------variables :: speed: experiencemethods :: shoot: hide: rush

Class :: Demolition---------------------variables :: strength: experiencemethods :: shoot: hide: detonate

SUPER-CLASS

SUB-CLASS

SUB-CLASS

SUB-CLASS

Page 25: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

Class :: Person---------------------variables :: name: age: gendermethods :: eat: sleep: playCS

SUPER-CLASS

Class :: Soldier---------------------variables :: experiencemethods :: shoot: hide

Class :: Sniper---------------------variables :: accuracymethods :: snipe

Class :: Rusher---------------------variables :: speedmethods :: rush

Class :: Demolition---------------------variables :: strengthmethods :: denotate

SUB-CLASS

SUB-CLASS

SUB-CLASS

SUB-CLASS

Page 26: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Polymorphism means the ability to takes more than one form.

• An operation may exhibit different behavior in different instances.

• The behavior depend upon the types of data used in the operation.

• Polymorphism allows objects having different internal structures to share the same external interface.

• Enables method over-riding ; created a same method to deal with many different data types.

1.4.6 Polymorphism

Page 27: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

Shape

Draw( )

Circle Object

Draw(circle)

Box object

Draw(box)

Triangle object

Draw(triangle)

Illustrates that a single function name can be used to handle different number and different types of arguments.

Page 28: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• Binding refers to the linking of a procedure call to the code to be executed in response to the call.

• Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.

1.4.7 Dynamic Binding

Page 29: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• An object-oriented program consists of a set of objects that communicate with each other.

• The process of programming in an object-oriented language therefore involves the following basic steps :

– Creating classes that define objects and their behavior.– Creating objects from class definitions.– Establishing communication among objects.

• Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another.

• The concept of message passing makes it easier to talk about building systems that directly model or simulate their real-world counterparts.

1.4.8 Message Communication

Page 30: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• In procedural language (Like C or Pascal)– You create a program by determining:

• The task you want your program to accomplish • Then figuring out the steps or procedures that are

needed to accomplish the task. • The code is organized into several functions, and each

function typically represents a sub-task. • In object oriented language (Like C++ or JAVA)

– You create a program by determining:• First, what objects the program will use to accomplish

its goal.• For each kind of object, you determine what data the

object needs to contain, and what messages the object should respond to.

1.5 Procedural vs. Object Oriented language

Page 31: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

Benefits of Object Oriented Programming

• Through inheritance, we can eliminate redundant code and extent the use of existing classes (reuseability).

• The principle of data hiding helps the programmer to build secure programs that cannot invaded by code in other parts of the program (encapsulation).

• Polymorphism allows interaction between objects without having to explicitly state which type of data we are using (abstraction).

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

• Object-oriented system can be easily upgraded from small to large systems (scaleability).

• Encouragement of good programming techniques because of clearer design and better communication by high level of abstraction.

Page 32: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• A real world situation goes as given below:

– Timothy wants to send some flowers to Sally.

– Timothy places an order for flowers to Flora, the florist.

– Flora delivers the flowers to Sally.

• The program flow in a procedural language will look like this:

function Tim( )

Flo(100) //Send $100

function Flo(amount) //Receive the amount

roses = getflower(amount) //Process the order

Sal(roses)

function Sal(flowers) //Receive flowers

//flowers get the value roses

Page 33: DCS 5085 Object Oriented Programming Chapter 1. Overview Introduction to Programming Programming Methodology Comparison of Programming Paradigms Basic

• The program flow in a object oriented language will look like this:

– Tim, Flo, Sal are all objects. (Class instantiation)

– Flo, being a florist is a subclass of a general class “ShopKeeper”. That is, she does all the transactions a general shopkeeper does, but has more specific details related to a florist. (Inheritance)

– Tim sends a message to Flo for ordering flowers. The object Flo can respond to such a message. (Behavior)

– A different subclass of “ShopKeeper”, say Ken, the artist, will respond differently to the same message by Tim. (Method overriding)

– Flo will process the order. Tim does not know what method Flo uses to honor his order (Encapsulation)

– Sal receives the flowers. (Object interaction).