Object Oriented Programming Group

handprintΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

55 εμφανίσεις

CS 331 Programming Languages

Date: 4/21/08

Object Oriented
Programming Group

Featuring:

James Webber II


Topics Include:

OOP

Smalltalk

CS 331 Programming Languages

Date: 4/21/08

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


Basically unique data type defined by programmer


Inheritance


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


Dynamic call of method calls to methods


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


Messages can be parameterized with variables
that reference objects


ALL objects referenced from heap and all objects
are implicitly dereferenced


No explicit dereferencing: there is a garbage
collection process


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


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

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

CS 331 Programming Languages

Date: 4/21/08

Inheritance Terminology


Object:

Class Instances


Method
: subprograms which define
operations on objects


Message:

call to a method


Message protocol:

entire collection of
methods


Overridden method:

a base class
method which is redefined in a derived
class

CS 331 Programming Languages

Date: 4/21/08

An Inheritance issue


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?


The Diamond Solution


Different languages solve this
problem in different ways


CS 331 Programming Languages

Date: 4/21/08

Access Control: What is inherited?


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


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


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

C++ terminology

CS 331 Programming Languages

Date: 4/21/08

Dynamic Binding


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


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

C++ terminology

CS 331 Programming Languages

Date: 4/21/08

Alan Kay


Designer of smalltalk


Wrote thesis on “dynabook” concept


Described laptops / tablet PCs (kind of)


Described small & portable computer


Described infinite battery life


Described purpose: teach children (target audience is not adults)


Performed studies on children


Children learn best kinetically with motion using images,
symbols, abstract representation


Heavy influence on OLPC (one laptop per child)


Employed by Xerox Parc at time Smalltalk Invented

CS 331 Programming Languages

Date: 4/21/08

Development of Smalltalk


Developed by Xerox Palo Alto Research
Center employees


Most notable are Alan Kay(design) and Adele
Goldberg (group leader/implementation
management)


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

CS 331 Programming Languages

Date: 4/21/08

Smalltalk Timeline:


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


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!


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


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


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


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


Message: most fundamental language construct


42 factorial “sends factorial to object 42”


2 raisedTo: 4 “4 is argument to raisedTo and this is passed into
2”


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!


http://www.squeak.org/


Supports: Windows,
Mac, Linux


Also Developed by Alan
Kay

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

C++

Smalltalk

Int a = 5;

a:=5

//comment

“comment”

;

.

return a;

^a

ClassName obj;

ClassName new

CS 331 Programming Languages

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


Currently has a large following, very
popular


Implemented in the OLPC as the primary
OS


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

CS 331 Programming Languages

Date: 4/21/08

Conclusion


OOP


ADTs


Inheritance


Dynamic Calls to Methods


SmallTalk


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


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/gst
-
manual/gst_34.html#SEC75


http://www.outbacksoftware.com/smalltalk/smal
ltalk.html


James Webber II: Squeak Images &
implementation