Object Oriented Programming Group Featuring James Webber II
Object Oriented Programming Group Featuring: James Webber II Topics Include: OOP Smalltalk CS 331 Programming Languages Date: 4/21/08
3 Key language features n Abstract Data Types n Inheritance n Dynamic call of method calls to methods – 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 – Basically unique data type defined by programmer – Bestow, add, or modify characteristics of an existing class by creating a subclass with the same basic characteristics – Basically means accessing methods of a subclass by using a base class’ pointer (aka polymorphism) CS 331 Programming Languages Date: 4/21/08
Smalltalk: The First OOPL Program consists entirely of objects & everything is treated uniformly n Messages can be parameterized with variables that reference objects n ALL objects referenced from heap and all objects are implicitly dereferenced n – No explicit dereferencing: there is a garbage collection process n Smalltalk is also a pure OOPL in that all data types are objects and all operations on them are methods CS 331 Programming Languages Date: 4/21/08
Inheritance n What is the point of inheritance? – Organize & Modify programs § Organize accessible data of subclasses § Modify existing data by redifining definitions of the superclass in a subclass Mammal -> primate -> human -> student -> CS student beast Remove sleep and finances Throw in intelligence Further increase intelligence at the cost of social skills and Increase intelligence remove 30 minutes of life (theoretically) CS 331 Programming Languages Date: 4/21/08
Inheritance Terminology n Object: Class Instances n Method: subprograms which define operations on objects n Message: call to a method n Message protocol: entire collection of methods n Overridden method: a base class method which is redefined in a derived class CS 331 Programming Languages Date: 4/21/08
An Inheritance issue n The Diamond Problem – Classes B and C both inherit from A – Class D inherits from both B and C – If method in D calls a method in A and both B and C have overriden the method, which method does D inherit? n The Diamond Solution – Different languages solve this problem in different ways CS 331 Programming Languages Date: 4/21/08
Access Control: What is inherited? n Public: publicly accessible members inherited from the base class stay publicly accessible in derived class n Protected: members are not accessible from outside the class except in derived classes (they remain protected there) n Private: public and protected members of the base class are made private in the derived class CS 331 Programming Languages C++ terminology Date: 4/21/08
Dynamic Binding n Polymorphic reference / pointer: when a pointer to a base class’ object has the capability to point to a derived class’ object – These are determined at run-time via the object’s type n Abstract method: a protocol that is within the body of the base class (a hollow shell) which is defined in the derived class – A class that includes at least one abstract method is called an abstract class CS 331 Programming Languages C++ terminology Date: 4/21/08
Alan Kay Designer of smalltalk n Wrote thesis on “dynabook” concept n – Described laptops / tablet PCs (kind of) § Described small & portable computer § Described infinite battery life § Described purpose: teach children (target audience is not adults) n Performed studies on children – Children learn best kinetically with motion using images, symbols, abstract representation – Heavy influence on OLPC (one laptop per child) n Employed by Xerox Parc at time Smalltalk Invented CS 331 Programming Languages Date: 4/21/08
Development of Smalltalk n Developed by Xerox Palo Alto Research Center employees – Most notable are Alan Kay(design) and Adele Goldberg (group leader/implementation management) n When people reference smalltalk, they normally reference the 1980 version dubbed “smalltalk-80” CS 331 Programming Languages Date: 4/21/08
Smalltalk Timeline: n n 1971 - Kay creates smalltalk due to bet: “page of code” message passing programming language 1971 -1972 - an actually researched smalltalk-72 is created End of 1976 - a development environment included (GUI) 1980 - first language made available outside of Xerox PARC – Added metaclasses (class whose instance is a class) – Apple, HP, universities called for “peer review” – Made ANSI standard in 1998 CS 331 Programming Languages Date: 4/21/08
Smalltalk’s Diamond solution • 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 CS 331 Programming Languages Date: 4/21/08
The Single Inheritance Life n The life of a message: – Message to object causes class to be searched for corresponding method dynamically § Search fails… go to superclass § Repeat until system superclass OBJECT – Has no superclass – If not found… ERROR! n The price of the message: – Due to this process being performed for everything dynamically (everything is an object) smalltalk programs are significantly slower than its comparable programs on different languages CS 331 Programming Languages Date: 4/21/08
Classes and Inheritance n. A subclass inherits all instance variables, instance methods and class methods of its super class – A subclass’ own instance variables must differ from those of superclass – if override method of superclass in a subclass then the superclass’ definition becomes hidden CS 331 Programming Languages Date: 4/21/08
Smalltalk Typechecking n The only method of typechecking is ensuring that a message matches some method n Variables aren’t typed: names can be bound to any object – Variable types are irrelevant as long as they are consistent – Meaning of operation on variable determined by class of the object to which it is bound CS 331 Programming Languages Date: 4/21/08
Methods, Messages, Blocks Method: class subprogram which defines operation on object n Message: most fundamental language construct n – 42 factorial “sends factorial to object 42” – 2 raised. To: 4 “ 4 is argument to raised. To and this is passed into 2” n Block: a block of code expressed as a literal value – An object that holds executable statements, what it does with the code is dependent on the context – [ : x | x+1 ] “the same as f(x)=x+1” – iftrue: [ ^somevalue]. – Iffalse: [^somevalue]. CS 331 Programming Languages Date: 4/21/08
Let’s Program w/ Squeak! n http: //www. squeak. org/C++ – Supports: Windows, Mac, Int a = 5; Linux //comment – Also Developed by Alan ; Kay Remember: everything is an object and Smalltalk has limited keywords! CS 331 Programming Languages Smalltalk a: =5 “comment”. return a; ^a Class. Name obj; Class. Name new Date: 4/21/08
First Thing’s First CS 331 Programming Languages Date: 4/21/08
The Confusion CS 331 Programming Languages Date: 4/21/08
Basic Stuff to Know CS 331 Programming Languages Date: 4/21/08
Class and Inheritance Example CS 331 Programming Languages Date: 4/21/08
Current Status and the Future of Smalltalk n Currently has a large following, very popular n Implemented in the OLPC as the primary OS n Multinational language support; open source SQUEAK is available for those who want to try it CS 331 Programming Languages Date: 4/21/08
Conclusion n OOP n Small. Talk – ADTs – Inheritance – Dynamic Calls to Methods – – First OOPL pure OOPL Contributed to modern GUI Everything is an object § As a result, everything runs slowly – Order of Operations: left to right regardless (parenthesis support) CS 331 Programming Languages Date: 4/21/08
References n n n n Sebesta: OOP, Smalltalk Basics http: //en. wikipedia. org/wiki/Diamond_problem http: //en. wikipedia. org/wiki/Alan_Kay http: //www. squeak. org/ http: //gnu. paracoda. com/software/smalltalk/gstmanual/gst_34. html#SEC 75 http: //www. outbacksoftware. com/smalltalk/smal ltalk. html James Webber II: Squeak Images & implementation CS 331 Programming Languages Date: 4/21/08
- Slides: 24