Greenfoot will be examined in your online 2 hr Unit 2 exam in two ways. You will be expected to write Greenfoot code, plus you will have questions about the basic concepts of Object Oriented Programming, also known as OOPs (Greenfoot is an Object Oriented Programming Language).
Classes & Objects
2. When a mushroom is added to the world, the class is copied and an instance of a mushroom class is created.
Many mushrooms can be created from a single class - we have created 9 mushroom objects (known as instances), in this world.
A class is like a template, used to create objects in a program.
Using the mushroom example, a mushroom class is set up under the superclass Actor.
Inheritance, Superclass & Subclass
In this example, World and Actor are known as a Superclass (or Base class). When a new class is derived from a super class, it is known as a Subclass. In this example, snakeWorld, Snake, Python and BoaConstrictor are all Subclasses.
When a new class is derived from another class, the new class INHERITS all the properties (data) and methods (processes) of the existing class. i.e. a copy is taken of the existing class and new properties and methods can then be added to the new class, if necessary.
Note: When Python and BoaConstrictor classes were created, they were based upon the Snake class, inheriting all the properties and methods of the Snake class
When snakeWorld was created, it was based upon the World class, , inheriting all the properties and methods of the World class
Encapsulation, Methods & Properties
Encapsulation is simply the process of wrapping up the properties (data, sometimes called variables) and methods (processes). It is a way of grouping properties and methods in a single class together.
If Otherobject wants to update data in Wombat, it can only do so by asking Wombat to update it's data using the methods contained within Wombat. This aids debugging, as only Wombat can change it's own data - if an error occurs, it is clear to see the object that updated the erroneous data.
Properties (private & public)
Variables (properties) can be Public or Private.
Private: these variables cannot be accessed from outside the class to which they belong
Public: these variables can be accessed through the class to which they belong
A good example of using public vs private properties is the Counter class.
Private:
Inside the Counter class, the value and target variables are used to add a number onto the Counter displayed in the world. They are currently private, so no other class can see them, or update them.
Public:
When the value and target variables changed to public, we can make a link with the counter class and access these variables in our code.
Parameters
Parameters are values that are given to procedures when they run. Parameters can change each time you run the procedure, so it is an efficient way of writing code.
Here is the code we write in our Actor:
Here is the code (procedure) being used in Counter class:
Each time we use(call) the add procedure, we have to also tell it how many we want to add on (or subtract). This number in brackets is known as a parameter.
Example 1: Add 1 to a Counter
Example 1: Add 5 to a Counter
Example 1: Subtract 2 from a Counter
Note 1: The parameter can be changed to any value, depending on what we want to do with the score displayed in the Counter class in the world.
Note 2: We can subtract as well as add. However the procedure name doesn't change - we still use the add procedure - we simply give it a negative parameter value (e.g. -2)
Abstraction
To make coding simpler, we encapsulate properties (data) and methods (processes) together. This means that some detail (non-essential features) within a class is hidden from other classes. Abstraction is the process of hiding details, so that the overall view of a system is simplified, essentially ignoring the characteristics of problems that are not needed in order to concentrate on those that are needed.
The wombat’s states (data) are hidden from other objects but its behaviours (methods) are not.
This could mean that another object could interact with the wombat grunting, burrowing or moving, but could not change the wombat's name, breed etc.
Polymorphism
Both the Python and BoaConstrictor are subclasses of the class Snake and inherit all properties and methods of the Snake class.
The Snake class will have a method called killPrey(). The Python class and the BoaConstrictor class will inherit the method killPrey().
However the Python will kill it's prey by poisoning it and the BoaConstrictor will kill it's prey by squeezing it to death.
Polymorphism is where the classes have the same behaviour (or method), but they implement the behaviour in different ways.
Comments in Greenfoot
Comments can be identified and used in several ways in Greenfoot:
To comment out a single line, use //
For example:
The code is greyed out, showing it will not be compiled/run.
To comment out multiple lines, use
/**
*
*
*/
For example:
This method is often used for procedure comments, but can be used for multiple lines of code.