Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object-Oriented Programming (Section 10.1-10.2) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.

Similar presentations


Presentation on theme: "1 Object-Oriented Programming (Section 10.1-10.2) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos."— Presentation transcript:

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

2 2 Data Abstraction and Object Orientation Control or process abstraction is a very old idea (subroutines!), though few languages provide it in a truly general form (Scheme comes close).Control or process abstraction is a very old idea (subroutines!), though few languages provide it in a truly general form (Scheme comes close). Data abstraction is somewhat newer, though its roots can be found in Simula67.Data abstraction is somewhat newer, though its roots can be found in Simula67. An Abstract Data Type is one that is defined (from the user’s point of view) in terms of the operations that it supports (i.e. that can be performed upon it) rather than in terms of its structure or implementation.An Abstract Data Type is one that is defined (from the user’s point of view) in terms of the operations that it supports (i.e. that can be performed upon it) rather than in terms of its structure or implementation.

3 3 Historical Development of Abstraction We’ve talked about data abstraction previously. Recall the historical development of data abstraction mechanisms:We’ve talked about data abstraction previously. Recall the historical development of data abstraction mechanisms: global set of variablesBasic localsFortran staticsFortran, Algol 60, C modulesModula-2, Ada 83 module typesEuclid objectsSmalltalk, C++, Eiffel, Java, Oberon, Modula-3, Ada 95 Oberon, Modula-3, Ada 95

4 4 Fundamental Concepts in OOP EncapsulationEncapsulation –Data Abstraction –Information hiding InheritanceInheritance –Code reusability –Is-a vs. has-a relationships PolymorphismPolymorphism –Dynamic method binding

5 5 Encapsulation Data abstraction allow programmers to hide data representation details behind a (comparatively) simple set of operations (an interface)Data abstraction allow programmers to hide data representation details behind a (comparatively) simple set of operations (an interface) What the benefits of data abstraction?What the benefits of data abstraction? –Reduces conceptual load »Programmers need to knows less about the rest of the program –Provides fault containment »Bugs are located in independent components –Provides a significant degree of independence of program components »Separate the roles of different programmer SoftwareEngineeringGoals

6 6 Encapsulation Classes, Objects and Methods The unit of encapsulation in an OO PL is a classThe unit of encapsulation in an OO PL is a class –An abstract data type »The set of values is the set of objects (or instances) Objects can have aObjects can have a –Set of instance attributes (has-a relationship) –Set of instance methods Classes can have aClasses can have a –Set of class attributes –Set of class methods Method calls are known as messages The entire set of methods of an object is known as the message protocol or the message interface of the objectThe entire set of methods of an object is known as the message protocol or the message interface of the object

7 7 Encapsulation Modules and Classes The basic unit of OO, the class, is a unit of scopeThe basic unit of OO, the class, is a unit of scope –This idea appeared in module-based languages in the mid- 70s »E.g. Clu, Modula, Euclid Rules of scope enforce data hidingRules of scope enforce data hiding –Names have to be exported in order to be accessible by other modules

8 8 Classes and Encapsulation Two Views Module-as-typeModule-as-type –A module is an abstract data type –Standardized constructor and destructor syntax –Object-oriented design is applied everywhere –E.g. Java, Smalltalk, Eiffel, C++, Python Module-as-managerModule-as-manager –A module exports an abstract data type –Create and destroy operations –Object-oriented design is optional (OO as an extension) –E.g. Ada 95, Modula-3, Oberon, CLOS, Perl

9 9 Visibility Rules Public and Private parts of an object declaration/definition.Public and Private parts of an object declaration/definition. 2 reasons to put things in the declaration2 reasons to put things in the declaration – so programmers can get at them – so the compiler can understand them At the very least the compiler needs to know the size of an object, even though the programmer isn't allowed to get at many or most of the fields (members) that contribute to that size. That's why private fields of an object have to be in the declaration.At the very least the compiler needs to know the size of an object, even though the programmer isn't allowed to get at many or most of the fields (members) that contribute to that size. That's why private fields of an object have to be in the declaration.

10 10 Access Restrictions C++ distinguishes amongC++ distinguishes among – public – protected – private class members. class members. Public means accessible to anybody.Public means accessible to anybody. Private means just to members of this class.Private means just to members of this class. Protected means to members of this or derived classes.Protected means to members of this or derived classes. A C++ struct is simply a class whose members are public by default.A C++ struct is simply a class whose members are public by 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 {... anybody can convert (assign) a circle* into a shape*

11 11 C++ List Example

12 12 C++ Example

13 13

14 14

15 15 Ada 95

16 16

17 17


Download ppt "1 Object-Oriented Programming (Section 10.1-10.2) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos."

Similar presentations


Ads by Google