Principles of Object-oriented Programming Programming Language Paradigms August 26, 2002
Pre-history 40s/50s – von Neumann architecture, stored program concept –Allows the development of general-purpose computing machines –Languages tend to be closely tied to the underlying architecture –Structured programming emerges Verb and task oriented Difficult to scale
History Simula-67 – objects, early GUI elements Xerox PARC (70s) – Smalltalk, bitmap displays, multiple windows, mouse, menus, icons, etc. Smalltalk-80 – modern version –released outside PARC. –Used a virtual machine and a bytecode compiler. –Spinoff companies create commercial versions.
Squeak History 1995: (Apple) – Alan Kay et al. create an open-source version of Smalltalk. –Development of a platform that can be used by people of all ages and skill levels. Squeak compiler and VM written in Squeak. Support for sound, 2-D and 3-D graphics, networking/web, Flash, etc.
Other OO history C++ developed at AT&T labs (Stroustrup:79) –Interested in an efficient language that extended C –Strong typing Objective C –Used in NeXT and OS X – combination of C and Smalltalk. Java developed by Sun ( ) –Initially developed as a language for communicating appliances/set-tops –Modified for use with WWW. –Smalltalk features in a typed environment
About Objects An ‘object’ is a way to capture both the features of a data structure and its behavior. Noun-oriented: encourages program design through thinking about pieces Construction of reusable elements
Classes and objects A class is a characterization of a type of object. –The Muppet class refers to the set of all muppets. –An object kermit is an instantiation of the Muppet class. Classes have: –Instance variables –Class variables –methods
An example Object subclass #Muppet instanceVariableNames: ‘name’ classVariableNames: ‘numberOfSpecies’ category: ‘muppets’ To create a muppet: Kermit <- [Muppet new].
Messages In Smalltalk, methods are invoked by sending the object a message. noun verb: arg1 mod: arg2 …. Modeled on speech acts.
Inheritance One of the most popular OO features is inheritance. –Can define new classes in terms of existing classes. –This creates a class hierarchy in which subclasses take on the behavior of their parent classes. A subclass can generate new behaviors by –Defining new methods –Overriding a parent method.
Inheritance example
Polymorphism Polymorphism is the idea that an object or method can perform the same operation on different data types. –Addition is polymorphic in most languages. Allows a programmer to write code without knowing the types of the data involved. [listOfStuff sort] <- don’t need to know what this is a list of.
Polymorphism example
Encapsulation Encapsulation provides a way of packing all the structure and behavior of an object into a single module. –Interface: the public face of an object –Implementation: How methods are carried out. Prevents users from misusing internal structure. Implementation can be changed without affecting external programs.
Encapsulation example