object oriented programming group

24
CS 331 Programming Languages Date: 4/21/08 Object Oriented Object Oriented Programming Group Programming Group Featuring: Featuring: James Webber II James Webber II Topics Include: Topics Include: OOP OOP Smalltalk Smalltalk

Upload: allene

Post on 14-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Object Oriented Programming Group. Featuring: James Webber II Topics Include: OOP Smalltalk. 3 Key language features. Abstract Data Types Requires abstraction: I don’t want to know what’s in a car that makes it work, it just needs to work with the definitions given to me - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Object Oriented Object Oriented Programming GroupProgramming Group

Featuring:Featuring:James Webber IIJames Webber II

Topics Include:Topics Include:OOPOOP

SmalltalkSmalltalk

Page 2: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

3 Key language features3 Key language features Abstract Data TypesAbstract Data Types

– Requires abstraction: I don’t want to know Requires abstraction: I don’t want to know what’s in a car that makes it work, it just what’s in a car that makes it work, it just needs to work with the definitions given to needs to work with the definitions given to meme

– Basically unique data type defined by Basically unique data type defined by programmerprogrammer

InheritanceInheritance– Bestow, add, or modify characteristics of an Bestow, add, or modify characteristics of an existing class by creating a subclass with existing class by creating a subclass with the same basic characteristicsthe same basic characteristics

Dynamic call of method calls to methodsDynamic call of method calls to methods– Basically means accessing methods of a Basically means accessing methods of a subclass by using a base class’ pointer (aka subclass by using a base class’ pointer (aka polymorphism)polymorphism)

Page 3: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Smalltalk: The First Smalltalk: The First OOPLOOPL

Program consists entirely of objects Program consists entirely of objects & everything is treated uniformly& everything is treated uniformly

Messages can be parameterized with Messages can be parameterized with variables that reference objectsvariables that reference objects

ALL objects referenced from heap and ALL objects referenced from heap and all objects are implicitly all objects are implicitly dereferenceddereferenced– No explicit dereferencing: there is a No explicit dereferencing: there is a garbage collection processgarbage collection process

Smalltalk is also a pure OOPL in Smalltalk is also a pure OOPL in that all data types are objects and that all data types are objects and all operations on them are methodsall operations on them are methods

Page 4: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

InheritanceInheritance

What is the point of inheritance?What is the point of inheritance?– Organize & Modify programsOrganize & Modify programs

Organize accessible data of subclassesOrganize accessible data of subclasses Modify existing data by redifining Modify existing data by redifining definitions of the superclass in a subclassdefinitions of the superclass in a subclass

Mammal -> primate -> human -> student -> CS student

Throw in intelligence

Increase intelligence (theoretically)

beast

Further increase intelligence at the cost of social skills and remove 30 minutes of life

Remove sleep and finances

Page 5: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Inheritance TerminologyInheritance Terminology

Object:Object: Class Instances Class Instances MethodMethod: subprograms which : subprograms which define operations on objectsdefine operations on objects

Message:Message: call to a method call to a method Message protocol:Message protocol: entire entire collection of methodscollection of methods

Overridden method:Overridden method: a base class a base class method which is redefined in a method which is redefined in a derived classderived class

Page 6: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

An Inheritance issueAn Inheritance issue The The Diamond ProblemDiamond Problem

– Classes B and C both Classes B and C both inherit from Ainherit from A

– Class D inherits from both Class D inherits from both B and CB and C

– If method in D calls a If method in D calls a method in A and both B and method in A and both B and C have overriden the C have overriden the method, which method does D method, which method does D inherit?inherit?

The Diamond SolutionThe Diamond Solution– Different languages solve Different languages solve this problem in different this problem in different waysways

Page 7: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Access Control: What is Access Control: What is inherited?inherited?

PublicPublic: publicly accessible members : publicly accessible members inherited from the base class stay inherited from the base class stay publicly accessible in derived publicly accessible in derived classclass

ProtectedProtected: members are not : members are not accessible from outside the class accessible from outside the class except in derived classes (they except in derived classes (they remain protected there)remain protected there)

PrivatePrivate: public and protected : public and protected members of the base class are made members of the base class are made private in the derived classprivate in the derived class

C++ terminology

Page 8: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Dynamic BindingDynamic Binding

Polymorphic reference / pointerPolymorphic reference / pointer: when a : when a pointer to a base class’ object has the pointer to a base class’ object has the capability to point to a derived class’ capability to point to a derived class’ objectobject– These are determined at run-time via the These are determined at run-time via the object’s typeobject’s type

Abstract methodAbstract method: a protocol that is : a protocol that is within the body of the base class (a within the body of the base class (a hollow shell) which is defined in the hollow shell) which is defined in the derived classderived class– A class that includes at least one abstract A class that includes at least one abstract method is called an abstract classmethod is called an abstract class

C++ terminology

Page 9: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Alan KayAlan Kay Designer of smalltalkDesigner of smalltalk Wrote thesis on “dynabook” conceptWrote thesis on “dynabook” concept

– Described laptops / tablet PCs (kind of)Described laptops / tablet PCs (kind of) Described small & portable computerDescribed small & portable computer Described infinite battery lifeDescribed infinite battery life Described purpose: teach children (target audience Described purpose: teach children (target audience is not adults)is not adults)

Performed studies on childrenPerformed studies on children– Children learn best kinetically with motion Children learn best kinetically with motion using images, symbols, abstract representationusing images, symbols, abstract representation

– Heavy influence on OLPC (one laptop per child)Heavy influence on OLPC (one laptop per child) Employed by Xerox Parc at time Smalltalk Employed by Xerox Parc at time Smalltalk InventedInvented

Page 10: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Development of Development of SmalltalkSmalltalk

Developed by Xerox Palo Alto Developed by Xerox Palo Alto Research Center employeesResearch Center employees– Most notable are Alan Kay(design) Most notable are Alan Kay(design) and Adele Goldberg (group and Adele Goldberg (group leader/implementation management)leader/implementation management)

When people reference When people reference smalltalk, they normally smalltalk, they normally reference the 1980 version reference the 1980 version dubbed “smalltalk-80”dubbed “smalltalk-80”

Page 11: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Smalltalk Timeline:Smalltalk Timeline: 1971- Kay creates smalltalk due to bet: 1971- Kay creates smalltalk due to bet: “page of code” message passing programming “page of code” message passing programming languagelanguage

1971-1972- an actually researched 1971-1972- an actually researched smalltalk-72 is createdsmalltalk-72 is created

End of 1976- a development environment End of 1976- a development environment included (GUI)included (GUI)

1980- first language made available 1980- first language made available outside of Xerox PARCoutside of Xerox PARC– Added metaclasses (class whose instance is a Added metaclasses (class whose instance is a class)class)

– Apple, HP, universities called for “peer Apple, HP, universities called for “peer review”review”

– Made ANSI standard in 1998Made ANSI standard in 1998

Page 12: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Smalltalk’s Diamond Smalltalk’s Diamond solutionsolution

•Recall that the diamond solution is different for every language

•Inheritance can be a real pain, keeping track of multiple inheritance can be worse if there are more overriden classes like B and C

•Smalltalk’s solution: let’s not deal with it… only single inheritance is allowed

Page 13: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

The Single Inheritance The Single Inheritance LifeLife

The life of a message:The life of a message:– Message to object causes class to be Message to object causes class to be searched for corresponding method searched for corresponding method dynamicallydynamically Search fails… go to superclassSearch fails… go to superclass Repeat until system superclass OBJECTRepeat until system superclass OBJECT

– Has no superclassHas no superclass– If not found… ERROR!If not found… ERROR!

The price of the message:The price of the message:– Due to this process being performed for Due to this process being performed for everything dynamicallyeverything dynamically (everything is an (everything is an object) smalltalk programs are object) smalltalk programs are significantlysignificantly slower than its comparable slower than its comparable programs on different languagesprograms on different languages

Page 14: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Classes and InheritanceClasses and Inheritance

A subclass inherits all instance A subclass inherits all instance variables, instance methods and variables, instance methods and class methods of its super classclass methods of its super class– A subclass’ own instance variables A subclass’ own instance variables mustmust differ from those of differ from those of superclasssuperclass

– if override method of superclass in if override method of superclass in a subclass then the superclass’ a subclass then the superclass’ definition becomes hiddendefinition becomes hidden

Page 15: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Smalltalk TypecheckingSmalltalk Typechecking

The only method of typechecking is The only method of typechecking is ensuring that a message matches ensuring that a message matches some methodsome method

Variables aren’t typed: names can Variables aren’t typed: names can be bound to any objectbe bound to any object– Variable types are irrelevant as Variable types are irrelevant as long as they are consistentlong as they are consistent

– Meaning of operation on variable Meaning of operation on variable determined by class of the object to determined by class of the object to which it is boundwhich it is bound

Page 16: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Methods, Messages, Methods, Messages, BlocksBlocks

Method: class subprogram which defines Method: class subprogram which defines operation on objectoperation on object

Message: most fundamental language Message: most fundamental language constructconstruct– 42 factorial “sends factorial to object 42”42 factorial “sends factorial to object 42”– 2 raisedTo: 4 “4 is argument to raisedTo and this 2 raisedTo: 4 “4 is argument to raisedTo and this is passed into 2”is passed into 2”

Block: a block of code expressed as a Block: a block of code expressed as a literal valueliteral value– An object that holds executable statements, what An object that holds executable statements, what it does with the code is dependent on the contextit does with the code is dependent on the context

– [ :x | x+1 ] “the same as f(x)=x+1”[ :x | x+1 ] “the same as f(x)=x+1”– iftrue: [ ^somevalue].iftrue: [ ^somevalue].– Iffalse:[^somevalue].Iffalse:[^somevalue].

Page 17: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Let’s Program w/ Let’s Program w/ Squeak!Squeak!

http://www.squeakhttp://www.squeak.org/.org/– Supports: Windows, Supports: Windows, Mac, LinuxMac, Linux

– Also Developed by Also Developed by Alan KayAlan Kay

Remember: Remember: everything is an everything is an object and object and Smalltalk has Smalltalk has limited keywords!limited keywords!

C++C++ SmalltalkSmalltalk

Int a = 5;Int a = 5; a:=5a:=5

//comment//comment ““comment”comment”

;; ..

return a;return a; ^a^a

ClassName ClassName obj;obj; ClassName newClassName new

Page 18: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

First Thing’s FirstFirst Thing’s First

Page 19: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

The Confusion

Page 20: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Basic Stuff to KnowBasic Stuff to Know

Page 21: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Class and Inheritance Class and Inheritance ExampleExample

Page 22: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

Current Status and the Current Status and the Future of SmalltalkFuture of Smalltalk

Currently has a large Currently has a large following, very popularfollowing, very popular

Implemented in the OLPC as the Implemented in the OLPC as the primary OSprimary OS

Multinational language support; Multinational language support; open source SQUEAK is available open source SQUEAK is available for those who want to try itfor those who want to try it

Page 23: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

ConclusionConclusion OOPOOP

– ADTsADTs– InheritanceInheritance– Dynamic Calls to MethodsDynamic Calls to Methods

SmallTalkSmallTalk– First OOPLFirst OOPL– pure OOPLpure OOPL– Contributed to modern GUIContributed to modern GUI– Everything is an objectEverything is an object

As a result, everything runs As a result, everything runs slowlyslowly– Order of Operations: left to right Order of Operations: left to right regardless (parenthesis support)regardless (parenthesis support)

Page 24: Object Oriented Programming Group

CS 331 Programming Languages Date: 4/21/08

ReferencesReferences Sebesta: OOP, Smalltalk BasicsSebesta: OOP, Smalltalk Basics http://en.wikipedia.org/wiki/Diamond_http://en.wikipedia.org/wiki/Diamond_problemproblem

http://en.wikipedia.org/wiki/Alan_Kayhttp://en.wikipedia.org/wiki/Alan_Kay http://www.squeak.org/http://www.squeak.org/ http://gnu.paracoda.com/software/http://gnu.paracoda.com/software/smalltalk/gst-manual/smalltalk/gst-manual/gst_34.html#SEC75gst_34.html#SEC75

http://www.outbacksoftware.com/http://www.outbacksoftware.com/smalltalk/smalltalk.htmlsmalltalk/smalltalk.html

James Webber II: Squeak Images & James Webber II: Squeak Images & implementationimplementation