lecture 21: object-oriented programming (section 10.1-10.2)

17
1 Lecture 21: Lecture 21: Object-Oriented Programming Object-Oriented Programming (Section 10.1-10.2) (Section 10.1-10.2) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2002 Fall 2002 A compilation of material A compilation of material developed by Felix Hernandez- developed by Felix Hernandez- Campos and Michael Scott Campos and Michael Scott

Upload: burton

Post on 05-Jan-2016

25 views

Category:

Documents


1 download

DESCRIPTION

Lecture 21: Object-Oriented Programming (Section 10.1-10.2). CSCI 431 Programming Languages Fall 2002. A compilation of material developed by Felix Hernandez-Campos and Michael Scott. Data Abstraction and Object Orientation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

11

Lecture 21: Lecture 21: Object-Oriented ProgrammingObject-Oriented Programming

(Section 10.1-10.2)(Section 10.1-10.2)

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2002Fall 2002

A compilation of material developed by Felix A compilation of material developed by Felix Hernandez-Campos and Michael ScottHernandez-Campos and Michael Scott

Page 2: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

22

Data Abstraction and Object Data Abstraction and Object OrientationOrientation

• Control or Control or processprocess abstraction is a very old idea abstraction is a very old idea (subroutines!), though few languages provide it in a (subroutines!), though few languages provide it in a truly general form (Scheme comes close).truly general form (Scheme comes close).

• Data abstraction is somewhat newer, though its roots Data abstraction is somewhat newer, though its roots can be found in Simula67.can be found in Simula67.

• An Abstract Data Type is one that is defined in terms An Abstract Data Type is one that is defined in terms of the operations that it supports (i.e. that can be of the operations that it supports (i.e. that can be performed upon it) rather than in terms of its performed upon it) rather than in terms of its structure or implementation.structure or implementation.

Page 3: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

33

Historical Development of Historical Development of AbstractionAbstraction

• We’ve talked about data abstraction previously. We’ve talked about data abstraction previously. Recall the historical development of abstraction Recall the historical development of abstraction mechanisms:mechanisms:

static set of variablesstatic set of variables BasicBasic

localslocals FortranFortran

staticsstatics Fortran, Algol 60, CFortran, Algol 60, C

modulesmodules Modula-2, Ada 83Modula-2, Ada 83

module typesmodule types EuclidEuclid

objectsobjects Smalltalk, C++, Eiffel, Java,Smalltalk, C++, Eiffel, Java,

Oberon, Modula-3, Ada 95Oberon, Modula-3, Ada 95

Page 4: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

44

Fundamental Concepts in OOPFundamental Concepts in OOP

• EncapsulationEncapsulation– Data AbstractionData Abstraction– Information hidingInformation hiding– The notion of class and objectThe notion of class and object

• InheritanceInheritance– Code reusabilityCode reusability– Is-a vs. has-a relationshipsIs-a vs. has-a relationships

• PolymorphismPolymorphism– Dynamic method bindingDynamic method binding

Page 5: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

55

EncapsulationEncapsulation

• Data abstractionData abstraction allow programmers to hide data allow programmers to hide data representation details behind a (comparatively) representation details behind a (comparatively) simple set of operations (an simple set of operations (an interfaceinterface))

• What the benefits of data abstraction?What the benefits of data abstraction?– Reduces Reduces conceptual loadconceptual load

» Programmers need to knows less about the rest of the programProgrammers need to knows less about the rest of the program

– Provides Provides fault containmentfault containment» Bugs are located in independent componentsBugs are located in independent components

– Provides a significant degree of Provides a significant degree of independenceindependence of program of program componentscomponents

» Separate the roles of different programmerSeparate the roles of different programmerSoftware Software

EngineeringEngineeringGoalsGoals

Page 6: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

66

EncapsulationEncapsulationClasses, Objects and MethodsClasses, Objects and Methods

• The unit of encapsulation in an OO PL is a The unit of encapsulation in an OO PL is a classclass– An abstract data typeAn abstract data type

» The set of values is the set of The set of values is the set of objectsobjects (or (or instancesinstances))

• Objects can have aObjects can have a– Set of Set of instanceinstance attributes attributes ((has-a relationshiphas-a relationship))– Set of Set of instanceinstance methodsmethods

• Classes can have aClasses can have a– Set of Set of class attributesclass attributes– Set of Set of class methodsclass methods

Method calls are Method calls are known as known as messagesmessages

• The entire set of methods of an object is known as the The entire set of methods of an object is known as the message protocolmessage protocol or the or the message interface message interface of the objectof the object

Page 7: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

77

EncapsulationEncapsulationModules and ClassesModules and Classes

• The basic unit of OO, the class, is a The basic unit of OO, the class, is a unit of scopeunit of scope– This idea originated in module-based languages in the This idea originated in module-based languages in the

mid-70smid-70s» E.g.E.g. Clu, Modula, Euclid Clu, Modula, Euclid

• Rules of scope enforce data hidingRules of scope enforce data hiding– Names have to be Names have to be exportedexported in order to be accessible by in order to be accessible by

other modulesother modules

Page 8: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

88

Classes and EncapsulationClasses and EncapsulationTwo ViewsTwo Views

• Module-as-typeModule-as-type– A module is an abstract data typeA module is an abstract data type– Standardized constructor and destructor syntaxStandardized constructor and destructor syntax– Object-oriented design is applied everywhereObject-oriented design is applied everywhere– E.g. Java, Smalltalk, Eiffel, C++, PythonE.g. Java, Smalltalk, Eiffel, C++, Python

• Module-as-managerModule-as-manager– A module exports an abstract data typeA module exports an abstract data type– Create and destroy operationsCreate and destroy operations– Object-oriented design is optional (OO as an extension)Object-oriented design is optional (OO as an extension)– E.g. Ada 95, Modula-3, Oberon, CLOS, PerlE.g. Ada 95, Modula-3, Oberon, CLOS, Perl

Page 9: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

99

Visibility RulesVisibility Rules

• Public and Private parts of an object Public and Private parts of an object declaration/definition.declaration/definition.

• 2 reasons to put things in the declaration2 reasons to put things in the declaration– so programmers can get at themso programmers can get at them– so the compiler can understand themso the compiler can understand them

• At the very least the compiler needs to know the size At the very least the compiler needs to know the size of an object, even though the programmer isn't of an object, even though the programmer isn't allowed to get at many or most of the fields allowed to get at many or most of the fields (members) that contribute to that size. That's why (members) that contribute to that size. That's why private fields of an object have to be in the private fields of an object have to be in the declaration.declaration.

Page 10: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1010

Access RestrictionsAccess Restrictions

• C++ distinguishes amongC++ distinguishes among– publicpublic– protectedprotected– privateprivate

class members. class members.

• PublicPublic means accessible to anybody. means accessible to anybody.

• PrivatePrivate means just to members of this class. means just to members of this class.

• ProtectedProtected means to members of this or derived classes. means to members of this or derived classes.

• A C++ struct is simply a class whose members are public by A C++ struct is simply a class whose members are public by default.default.

• C++ base classes can also be public, private, or protected. E.g.C++ base classes can also be public, private, or protected. E.g.class circle : public shape { ...class circle : public shape { ...anybody can convert (assign) a circle* into a shape*anybody can convert (assign) a circle* into a shape*

Page 11: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1111

C++ List ExampleC++ List Example

Page 12: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1212

C++ ExampleC++ Example

Page 13: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1313

C++ ExampleC++ Example

Page 14: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1414

C++ ExampleC++ Example

Page 15: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1515

Ada 95Ada 95

Page 16: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1616

Ada 95Ada 95

Page 17: Lecture 21:  Object-Oriented Programming (Section 10.1-10.2)

1717

Ada 95Ada 95