introducing object-oriented programming (oop)

23
CSCI N201: CSCI N201: Programming Concepts Programming Concepts Copyright Copyright ©2005 ©2005 Department of Computer & Information Science Department of Computer & Information Science Introducing Object- Introducing Object- Oriented Programming Oriented Programming (OOP) (OOP)

Upload: francois-lesage

Post on 30-Dec-2015

55 views

Category:

Documents


7 download

DESCRIPTION

Introducing Object-Oriented Programming (OOP). Goals. By the end of this lecture, you should … Understand the three pillars of Object-Oriented Programming: Inheritance, Encapsulation and Polymorphism. Understand what an object is. Understand object attributes, methods and events. - PowerPoint PPT Presentation

TRANSCRIPT

CSCI N201:CSCI N201: Programming ConceptsProgramming Concepts

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Introducing Object-Introducing Object-Oriented Programming Oriented Programming (OOP)(OOP)

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

GoalsGoals

• By the end of this lecture, you should …By the end of this lecture, you should …• Understand the three pillars of Object-Understand the three pillars of Object-

Oriented Programming: Inheritance, Oriented Programming: Inheritance, Encapsulation and Polymorphism.Encapsulation and Polymorphism.

• Understand what an object is. Understand what an object is. • Understand object attributes, methods Understand object attributes, methods

and events.and events.• Understand how programmers use APIs.Understand how programmers use APIs.

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Programming LanguagesProgramming Languages

• Programming languages allow Programming languages allow programmers to code software.programmers to code software.

• The three major families of The three major families of languages are:languages are:– Machine languagesMachine languages– Assembly languagesAssembly languages– High-Level languagesHigh-Level languages

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Machine LanguagesMachine Languages

• Comprised of 1s and 0sComprised of 1s and 0s• The “native” language of a computerThe “native” language of a computer• Difficult to program – one misplaced Difficult to program – one misplaced

1 or 0 will cause the program to fail.1 or 0 will cause the program to fail.• Example of code:Example of code:1110100010101 111010101110 1110100010101 111010101110 10111010110100 1010001111011110111010110100 10100011110111

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Assembly LanguagesAssembly Languages

• Assembly languages are a step towards Assembly languages are a step towards easier programming. easier programming.

• Assembly languages are comprised of a Assembly languages are comprised of a set of elemental commands which are tied set of elemental commands which are tied to a specific processor.to a specific processor.

• Assembly language code needs to be Assembly language code needs to be translated to machine language before the translated to machine language before the computer processes it.computer processes it.

• Example:Example:ADD 1001010, 1011010ADD 1001010, 1011010

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

High-Level LanguagesHigh-Level Languages

• High-level languages represent a giant High-level languages represent a giant leap towards easier programming.leap towards easier programming.

• The syntax of HL languages is similar The syntax of HL languages is similar to English. to English.

• Historically, we divide HL languages Historically, we divide HL languages into two groups:into two groups:– Procedural languagesProcedural languages– Object-Oriented languages (OOP)Object-Oriented languages (OOP)

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Procedural LanguagesProcedural Languages

• Early high-level languages are Early high-level languages are typically called procedural languages.typically called procedural languages.

• Procedural languages are Procedural languages are characterized by sequential sets of characterized by sequential sets of linear commands. The focus of such linear commands. The focus of such languages is on languages is on structurestructure..

• Examples include C, COBOL, Fortran, Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScriptLISP, Perl, HTML, VBScript

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Object-Oriented LanguagesObject-Oriented Languages

• Most object-oriented languages are Most object-oriented languages are high-level languages.high-level languages.

• The focus of OOP languages is not on The focus of OOP languages is not on structure, but on structure, but on modeling datamodeling data..

• Programmers code using “blueprints” Programmers code using “blueprints” of data models called of data models called classesclasses..

• Examples of OOP languages include Examples of OOP languages include C++, Visual Basic.NET and Java.C++, Visual Basic.NET and Java.

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Object Oriented Object Oriented ProgrammingProgramming

• ObjectObject – Unique programming entity – Unique programming entity that has that has methodsmethods, has , has attributes attributes and and can react to can react to eventsevents..

• MethodMethod – Things which an object – Things which an object can do; the “verbs” of objects. In can do; the “verbs” of objects. In code, usually can be identified by an code, usually can be identified by an “action” word -- “action” word -- HideHide,, Show Show

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Object Oriented Object Oriented ProgrammingProgramming

• AttributeAttribute – Things which describe an – Things which describe an object; the “adjectives” of objects. In object; the “adjectives” of objects. In code, usually can be identified by a code, usually can be identified by a “descriptive” word – “descriptive” word – EnabledEnabled, , BackColorBackColor

• EventsEvents – Forces external to an object – Forces external to an object to which that object can react. In code, to which that object can react. In code, usually attached to an event procedureusually attached to an event procedure

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Object Oriented Object Oriented ProgrammingProgramming

• ClassClass – Provides a way to create new – Provides a way to create new objects based on a “meta-definition” objects based on a “meta-definition” of an object (Example: The of an object (Example: The automobile automobile classclass))

• ConstructorsConstructors – Special methods – Special methods used to create new instances of a used to create new instances of a class (Example: A Honda Civic is an class (Example: A Honda Civic is an instanceinstance of the automobile of the automobile classclass.).)

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

OOP - EncapsulationOOP - Encapsulation

• Incorporation into a class of data Incorporation into a class of data & operations in one package& operations in one package

• Data can only be accessed Data can only be accessed through that packagethrough that package

• ““Information Hiding”Information Hiding”

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

OOP - InheritanceOOP - Inheritance

• Allows programmers to create new Allows programmers to create new classes based on an existing classclasses based on an existing class

• Methods and attributes from the Methods and attributes from the parent class are inherited by the parent class are inherited by the newly-created classnewly-created class

• New methods and attributes can be New methods and attributes can be created in the new class, but don’t created in the new class, but don’t affect the parent class’s definitionaffect the parent class’s definition

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

OOP - PolymorphismOOP - Polymorphism

• Creating methods which describe Creating methods which describe the way to do some general the way to do some general function (Example: The “drive” function (Example: The “drive” method in the automobile class)method in the automobile class)

• Polymorphic methods can adapt Polymorphic methods can adapt to specific types of objects.to specific types of objects.

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Classes and ObjectsClasses and Objects

• A A classclass is a data type that allows programmers is a data type that allows programmers to create objects. A class provides a definition to create objects. A class provides a definition for an object, describing an object’s attributes for an object, describing an object’s attributes (data) and methods (operations).(data) and methods (operations).

• An object is an An object is an instanceinstance of a class. With one of a class. With one class, you can have as many objects as required.class, you can have as many objects as required.

• This is analogous to a variable and a data type, This is analogous to a variable and a data type, the class is the data type and the object is the the class is the data type and the object is the variable.variable.

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Sample Class DefintionSample Class DefintionClass CubeClass Cube

Side As RealSide As RealVolume As RealVolume As RealSubprogram SetSide(NewSide)Subprogram SetSide(NewSide)

Set Side = NewSideSet Side = NewSideEnd SubprogramEnd Subprogram

Subprogram ComputeVolume()Subprogram ComputeVolume()Set Volume = Side ^ 3Set Volume = Side ^ 3End SubprogramEnd Subprogram

Function GetVolume() As RealFunction GetVolume() As RealSet GetVolume = VolumeSet GetVolume = VolumeEnd FunctionEnd Function

Function GetSide() As RealFunction GetSide() As RealSet GetSide = SideSet GetSide = SideEnd FunctionEnd Function

End ClassEnd Class

The class Cube is similar The class Cube is similar to the definition of a to the definition of a record, but also has record, but also has functions added that are functions added that are part of the objects part of the objects created. So we can think created. So we can think of it as an object ‘has’ of it as an object ‘has’ data attributes and data attributes and function attributesfunction attributes

The class Cube is similar The class Cube is similar to the definition of a to the definition of a record, but also has record, but also has functions added that are functions added that are part of the objects part of the objects created. So we can think created. So we can think of it as an object ‘has’ of it as an object ‘has’ data attributes and data attributes and function attributesfunction attributes

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Sample Instance of ClassSample Instance of ClassMain ProgramMain Program

Declare Cube1 As CubeDeclare Cube1 As Cube

Write “Enter a positive number:”Write “Enter a positive number:”

Input Side1Input Side1

Call Cube1.SetSide(Side1)Call Cube1.SetSide(Side1)

Call Cube1.ComputeVolumeCall Cube1.ComputeVolume

Write “The volume of a cube of side ”, Write “The volume of a cube of side ”, Cube1.GetSideCube1.GetSide

Write “is ”, Cube1.GetVolumeWrite “is ”, Cube1.GetVolume

End ProgramEnd Program

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Is JavaScript an OOP language?Is JavaScript an OOP language?

• Well, not really …Well, not really …• We call JavaScript an "object-We call JavaScript an "object-

inspired" languageinspired" language• It uses objects by way of supporting It uses objects by way of supporting

inheritance and encapsulation, but it inheritance and encapsulation, but it doesn't really provide support for doesn't really provide support for polymorphism.polymorphism.

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

““Object-Oriented” JavaScriptObject-Oriented” JavaScript

• More like “Object-Inspired” JavaScriptMore like “Object-Inspired” JavaScript• We can create new, custom, re-usable We can create new, custom, re-usable

objects in JavaScript that include their objects in JavaScript that include their own methods, properties and events.own methods, properties and events.

• Consider the following problem: “I want Consider the following problem: “I want to record the color, brand, horsepower to record the color, brand, horsepower and price of several cars.”and price of several cars.”

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Solution without OOP Solution without OOP DesignDesign

• Uses parallel arraysUses parallel arrays• Can be confusingCan be confusing

– Difficult to keep track of which car has which Difficult to keep track of which car has which color, brand, etc.color, brand, etc.

• Example:Example:

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

What is an API?What is an API?

• API stands for Application API stands for Application Programming InterfaceProgramming Interface

• Allows programmers to extend the Allows programmers to extend the current language to included current language to included customized componentscustomized components

• Most modern languages incorporate Most modern languages incorporate APIsAPIs

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Solution with OOP DesignSolution with OOP Design

• Calls to an API that contains the Calls to an API that contains the custom car objectcustom car object

• Much cleaner codeMuch cleaner code• Re-usable objectRe-usable object

CSCI N201: Programming ConceptsCSCI N201: Programming ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Questions?Questions?