In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Object-Oriented Programming

SandCastleIcon.png This page has links to outside of the Scratch website or to Wikipedia. Always remember to be safe when using the internet since we can't guarantee the safety of other sites.

Object-Oriented Programming, often shortened to OOP, is a fundamental computer science paradigm. It revolves around data structures called Objects, which consist of states and behaviors, and the interaction between them via message passing.[1][2][3]

Class-based OOP

Class-based OOP allows the creation of classes, which define data types known as objects. An object consists of several key-value pairs. An instance of an object is a representation of an object stored in memory. An object can be stored as a variable, and its values can be accessed by searching with the appropriate key.

Prorotype-based OOP

Prorotype-based OOP is a form of object-oriented programming that involves the decoration of objects.[4][5] Instead of classes, objects are based on other objects.


A key feature of OOP is inheritance. In a class-based OOP system a class may inherit properties from another class. The inheriting class is called the "child" and the inherited class is called the "parent". If the class A was the parent to class B in an inheritance, we say "A" is the superclass to B and B is a subclass of A.

Warning Note: Notice how we say A is the superclass while B is a subclass. This is because a class may have many subclasses but inherits from only one superclass.

For example, the class "VW Beetle" can inherit from "Car", specifying the car class to VW Beetle.

A more real-world example is that of morphs. A Morph always inherits from another parent morph. So a ButtonMorph inherits from Morph, CancelButtonMorph inherits from ButtonMorph, etc. Similarly, ResizeHandleMorph inherits from DraggableMorph which inherits from Morph.


Encapsulation is the hiding of information. In object-oriented programming, an object's attributes may be hidden to prevent illegal state changes. Encapsulation is also used hide implementation details from the user[6]Accessor methods (often also called Getter methods) return the value of an attribute, and Mutator methods (often also called Setter methods) allow for modification of a hidden attribute.

Is Scratch OOP?

Whether or not Scratch is Object-oriented is disputable. It definitely uses objects, particularly sprite that can dynamically generate sprites through limited cloning capabilities. The following lists some basic arguments over the matter:

Arguments against Scratch being OOP are:

  • Scratch does not support custom objects
  • Scratch does not support dynamic generation of sprites

Arguments for Scratch being OOP are:

  • Scratch supports sprites, which can be considered objects.
  • You can access properties of sprites from other sprites.
  • Scratch 2.0 supports cloning, so dynamic sprite generation is possible.
  • Lists can be used to create pseudo-objects.

Using text lists for OOP

Lists can be used to support custom objects. A list will act as a class, and each item will represent an instance. To begin, create a list called "Object". Then add the following code:

when gf clicked
add [key/val/key2/val2] to [Object v]

Replace key and key2 with your keys and val and val2 with your values. This registers your instance with the Object class.

To reference an object, we must parse (decode) it. To decode an object, use this code:

define decode object (instance)
set [n v] to (0)
delete (all v) of [Obj-decoded v]
repeat until <(n) = (length of (item (instance) of [Objects v]))>
change [n v] by (1)
if <(letter (n) of (item (instance) of [Objects v]))=[/]> then
add [] to [Obj-decoded v]
replace item (last v) of [Obj-decoded v] with (join (item (last v) of [Obj-decoded v]) (letter (n) of (item (instance) of [Objects v])))

Finally, to reference an object, use this code:

set [n v] to [2]
set [key v] to [key]//or whatever
repeat until <(item (n) of [Obj-decoded v])=[key]>
change [n v] by (2)
set [value v] to (item ((n)+(1)) of [Obj-decoded v])

Class-based OOP With Lists

In this way of implementation, each key of an object is a list. The naming scheme we will use is Class.attribute.

First, write a constructor for the class.

define new ExampleClass key1: (arg1) key2: (arg2)
add (arg1) to [ExampleClass.key1 v]
add (arg2) to [ExampleClass.key2 v]
add [init value] to [ExampleClass.keyn v]
set [new ExampleClass v] to (length of [ExampleClass.key1 v]) //The length of any attribute list of the class will work

This script allocates a new instance of the class and returns a pointer to it, the list index of the items added to the attribute lists.

Methods may be written like this:

define ExampleClass (this).setKey1(val)
replace item (this) of [ExampleClass.key1 v] with (val)

"This" is the pointer to the instance to operate on.

Data fields may be accessed with the list item block:

(item (pointer to object) of [ExampleClass.key1 v])

This expression may be chained:

(item (item (instance) of [Class1.meow v]) of [Class2.woof v])

is equivalent to instance.meow.woof in other languages, where instance is an instance of Class1, which has a data field of the type Class2 called meow, and Class2 has a data field called woof.

OOP in Snap!

In the Scratch modification Snap!, there are many features which allow OOP: mainly the concept of lambda. To create a class in Snap!, one would create a procedure which reports another procedure. This reported procedure is the object. It takes in a message as an argument, and based on the message provided, returns the appropriate value or method. The following script creates a Counter class, with methods to increment, decrement, or just retrieve the value of the counter.

set [Counter v] to ({
comment: ["Value" is the counter's value]::grey
script variables ((value)::grey)@delInput@addInput::grey
set [value v] to [0]
comment: [This is the actual script to report:]::grey
report ({
comment: [Based on the key, do something]::grey
if <(key) = [increment]> then
change [value v] by [1]
if <(key) = [decrement]> then
change [value v] by [-1]
if <(key) = [retrieve]> then
report (value)::control cap
comment: [No matter what, you need to report the value]::grey
report (value)::control cap
}input names: ((key)::grey) ::ring grey)::control
}::ring grey)
comment: [Create a counter]::grey
set [My Counter v] to (call(Counter)@delInput@addInput::control)
comment: [Increment the counter and say its value]::grey
say (call (My Counter) with inputs [increment] ::control) for (2) secs
Warning Note: The "comment: []" block used here is a custom block which explicitly does nothing for the purpose of inline commenting.

This implementation of OOP is possible because of closures. A procedure may "capture" variables that are bound outside the lambda expression, and they will continue to exist, and can be referenced and assigned to.

OOP in Squeak

Document.png Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page.
Main article: Squeak Tutorial#Classes and instances


  • This page was last modified on 23 September 2016, at 23:43.