|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.
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.
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.
|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 userAccessor 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 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]))=[/]> add  to [Obj-decoded v] else 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]))) end end
Finally, to reference an object, use this code:
set [n v] to  set [key v] to [key]//or whatever repeat until <(item (n) of [Obj-decoded v])=[key]> change [n v] by (2) end set [value v] to (item ((n)+(1)) of [Obj-decoded v])
OOP in BYOB
|This article or section uses images made with Snap!, a Scratch Modification which allows script formatting. Differences include block multilining and zebra coloring.|
In the Scratch Modification BYOB, there are many features which allow OOP: mainly the concept of lambda. To create a class in BYOB, one would create a function which reports another function. This reported function is the object. It takes in a key as an argument, and based on the key provided, returns the appropriate value. The following script creates a Counter class, with methods to increment, decrement, or just retrieve the value of the counter.
|Note:||The "// " block used here is a custom block which explicitly does nothing for the purpose of inline commenting.|
OOP in Squeak
|Please expand this article or section. You can help by talk page.if you are an editor. More information might be found in a section of the|
- Main article: Squeak Tutorial#Classes and instances