32
1 INTRODUCTION TO OOP ective: ow the difference between functional program OOP now basic terminology in OOP now the importance of OOP now four design principles of OOP now OOP programming languages

1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Embed Size (px)

Citation preview

Page 1: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

1

INTRODUCTION TO OOP

Objective:

• 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 2: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

CSCI N201: Programming Concepts

Copyright ©2005 Department of Computer & Information Science

Introducing Object-Oriented Programming (OOP)

Page 3: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Programming LanguagesProgramming languages allow programmers to code software.The three major families of languages are:

Machine languagesAssembly languagesHigh-Level languages

Page 4: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Machine LanguagesComprised of 1s and 0sThe “native” language of a computerDifficult to program – one misplaced 1 or 0 will cause the program to fail.Example of code:1110100010101 111010101110 10111010110100 10100011110111

Page 5: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Assembly LanguagesAssembly languages are a step towards easier programming. Assembly languages are comprised of a set of elemental commands which are tied to a specific processor.Assembly language code needs to be translated to machine language before the computer processes it.Example:ADD 1001010, 1011010

Page 6: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

High-Level LanguagesHigh-level languages represent a giant leap towards easier programming.The syntax of HL languages is similar to English. Historically, we divide HL languages into two groups:

Procedural languagesObject-Oriented languages (OOP)

Page 7: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Procedural LanguagesEarly high-level languages are typically called procedural languages.Procedural languages are characterized by sequential sets of linear commands. The focus of such languages is on structure.Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScript

Page 8: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Object-OrientationA thinking methodology

Everything is an object.Any system is composed of objects (a system is also an object).The evolution and development of a system is caused by the interactions of the objects inside/outside a system.

Page 9: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Everything is an object

A student, a professorA desk, a chair, a classroom, a buildingA university, a city, a countryThe world, the universeA subject such as CS, IS, Math, History, …

Page 10: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Systems are composed of objects

An educational systemAn economic systemAn information systemA computer system

Page 11: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Object-Oriented Languages

Most object-oriented languages are high-level languages.The focus of OOP languages is not on structure, but on modeling data.Programmers code using “blueprints” of data models called classes.Examples of OOP languages include C++, Visual Basic.NET and Java.

Page 12: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Object Oriented Programming

Object – Unique programming entity that has methods, has attributes and can react to events.Method – Things which an object can do; the “verbs” of objects. In code, usually can be identified by an “action” word -- Hide, Show

Page 13: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Object Oriented Programming

Attribute – Things which describe an object; the “adjectives” of objects. In code, usually can be identified by a “descriptive” word – Enabled, BackColorEvents – Forces external to an object to which that object can react. In code, usually attached to an event procedure

Page 14: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

Object Oriented Programming

Class – Provides a way to create new objects based on a “meta-definition” of an object (Example: The automobile class)Constructors – Special methods used to create new instances of a class (Example: A Honda Civic is an instance of the automobile class.)

Page 15: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

15

STRUCTURED vs. OO PROGRAMMING

STRUCTURED PROGRAMMING:MAIN PROGRAM

FUNCTION 3FUNCTION 2

GLOBAL DATA

FUNCTION 5FUNCTION 4

FUNCTION 1

Page 16: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

16

Structured Programming

Using functionFunction & program is divided into modulesEvery module has its own data and function which can be called by other modules.

Page 17: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

17

OBJECT ORIENTED PROGRAMMING

Object 1 Object 2

Data

Function

Data

Function

Object 3

Data

Function

Page 18: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

18

OBJECT ORIENTED PROGRAMMING

Objects have both data and methods Objects of the same class have the same data

elements and methods Objects send and receive messages to invoke

actions

Key idea in object-oriented: 

The real world can be accurately described as a collection of objects that interact.

Page 19: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

19

Basic terminology

object - usually a person, place or thing (a noun)method

- an action performed by an object (a verb)attribute

- description of objects in a classclass

- a category of similar objects (such as automobiles)- does not hold any values of the object’s attributes

Page 20: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

20

Example for attributes and methods

Attributes:manufacturer’s namemodel nameyear madecolornumber of doorssize of engineetc.

Methods:Define data items (specify manufacturer’s name, model, year, etc.)Change a data item (color, engine, etc.)Display data itemsCalculate costetc.

Page 21: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

21

Why OOP?

Save development time (and cost) by reusing code

once an object class is created it can be used in other applications

Easier debuggingclasses can be tested independentlyreused objects have already been tested

Page 22: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

22

Why OOP (Cont.)Effects of OO methodology on software design

maintenanceextensibilityreusability

Page 23: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

23

Design Principles of OOP

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

Encapsulation Abstraction Polymorphism Inheritance

Page 24: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

24

Encapsulation

Also known as data hidingOnly object’s methods can modify

information in the object.

Analogy: ATM machine can only update accounts

of one person or object only.

Page 25: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

25

AbstractionFocus only on the important facts about the problem at handto 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 26: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

26

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 moneymove -

Page 27: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

27

Function Overloading

The operation of one function depends on the argument passed to it.

Example: Fly(), Fly(low), Fly(150)

Page 28: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

28

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 29: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

29

An Inheritance Hierarchy

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

Page 30: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

30

Object-Oriented Programming Languages

·        Pure OO Languages

Smalltalk, Eiffel, Actor, Java

 

·        Hybrid OO Languages

C++, Objective-C, Object-Pascal

Page 31: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

31

Review: Introduction to Object Orientation

What are the four basic principles of object orientation? Provide a brief description of each.What is an Object and what is a Class? What is the difference between them?What is an Attribute?What is an Operation?What is inheritance? What is polymorphism? Describe the strengths of object orientation.

Page 32: 1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know

32

Review: Introduction to Object Orientation

State 2 differences between functional programming and OOP.What are the four basic principles of object orientation? Provide a brief description of each.What is an Object and what is a Class? What is the difference between them?What is an Attribute?What is an Operation?Describe the strengths of object orientation.