2.1. Objects - Instances of Classes

In this unit, you will learn to use objects (variables of a class type) that have been designed by other programmers. Later on, in Unit 5, you will learn to create your own classes and objects.

2.1.1. Intro to Objects with Turtles

In the 1960s, an educational programming language called Logo was developed. It is best known for teaching programming with turtles! The turtles were graphical or robotic turtles that were controlled with simple commands like go forward or turn right. Here’s a photo of a robot turtle from the 1960s. The turtle had a pen attached to it. The student-programmers would steer the robot around using simple commands to create drawings with their code.

Children playing with a Logo turtle robot that can draw with a pen

Figure 1: Children playing with a Logo turtle robot that could draw with a pen

Today, we can play with virtual turtles in a graphical world. Below is a sample Java program that works with Turtle objects. Try clicking the run button button below to see what the following program does. (If the code below does not work for you, you can also see the Turtle code in action at this repl.it link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.)

In the program above, there is a Turtle object called yertle. The hidden Java code defines a complicated class called Turtle, World, and some other helper classes.

A class in programming defines a new abstract data type. When you create objects in coding, you create new variables of that class data type. Here, yertle is an object created from the class Turtle.

The class Turtle defines attributes (properties or variables) and methods (behaviors or functions) that each turtle can use. The dot operator (.) is used to run an object’s method. You can think of the . as an apostrophe s (‘s), for example run yertle’s forward method. The parentheses () after method names are there in case you need to give the method arguments (some data) to do its job, for example go forward 100 pixels.

Here is a class diagram that shows some of the attributes and methods inherited from the SimpleTurtle class in the class Turtle.

Turtle class diagram

Figure 2: Turtle Class Diagram

coding exercise Coding Exercise

In the code below, yertle goes forward and then turns left. Can you change the code to make yertle go forward twice and then turnRight?

When you write a class like the Turtle class, you can declare many object variables of that class type. In the code below, two turtle objects are created.

coding exercise Coding Exercise:

In the code below, two turtle object variables are created, yertle and myrtle. Can you add another turtle object to the code below? You can make up a variable name for your turtle and add in a line like the following in the main method and the make it move:

// To create or declare a new object, write:
// ClassName variableName = new ClassName(arguments);
Turtle yourTurtleName = new Turtle(world);

2.1.2. What are Classes and Objects?

In Java, a class is used to define a new data type (classify something). The class defines what objects of the class need to know (attributes or instance variables) and do (behaviors or methods). A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.

There are many classes that are part of the Java language, but you only have to know a few of these for the AP CS A exam (String, Math, System, ArrayList).

The real power of Java is the ability to create your own classes (define your own types). You will learn how to create your own classes in Unit 5.

Here’s a Greenfoot video, which was created using the free software Greenfoot (http://greenfoot.org), where you can see ant objects doing actions like moving and eating. Greenfoot makes it easy to create 2d simulations and games in Java. See http://www.greenfoot.org/doc/tut-2 for a tutorial if you are interested in learning more about Greenfoot.

You can think of a class like a cookie cutter. It is used to create the cookies (objects) and can be used to create as many cookies (objects) as you want. A class can also be thought of as a factory that produces objects.


Figure: Using a cookie cutter to make cookies

You can think of a class as the type or classification. The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat.


Figure: Pictures of cats (cat objects)

If you go to a restaurant, you will be seated by the greeter, the waiter will take your order, and the chef will cook your food. What do we mean by a greeter, waiter, and chef? Those are classifications or types of workers in a restaurant. Java has this same concept. When we create a new class we are defining a new type (a new classification) to the computer. Each type can have abilities or behaviors (called methods in Java) and attributes (called instance variables in Java). After you define a type, you can use it to create objects of that type. All objects created from a class will have the properties and abilities/behaviors defined in that class. For example, all turtle objects will know how to move forward and turn.

exercise Check your understanding

    2-1-1: How many objects can you create from a class in Java?

  • 1
  • There is one definition of a class, but the class can create as many objects as are needed.
  • 10
  • There is no limit on the number of objects you can create from a class.
  • 1000
  • There is no limit on the number of objects you can create from a class.
  • As many as you need
  • You can create as many objects as you need from one class.

    2-1-2: What specifies the behavior for objects of a class in Java?

  • attributes
  • attributes specify the data that an object keeps track of.
  • methods
  • Methods specify the behavior of all objects of a class.
  • class
  • While the class does specify the behavior of all objects created by that class, what part of a class specifies the behavior?
  • object
  • The object behavior is specified by the methods in the class that created the object.

    2-1-3: What specifies the data or state for an object in Java?

  • attributes
  • attributes specify the data that an object keeps track of.
  • methods
  • Methods specify the behavior of all objects of a class.
  • class
  • While the class does specify the data or state that all objects of the class keep track of, what part of the class stores the data?
  • object
  • The object data or state is stored in the attributes of the object. The attributes are defined in the class.

2-1-4: What are some attributes of cats? What are some behaviors of cats? (Note that attributes are often nouns or adjectives describing features of cats, and behaviors are often verbs).

2.1.3. groupwork Programming Challenge : Turtle Drawing

We encourage you to work in pairs for this challenge.

Create a Turtle object below and have it draw a shape. For example, have it draw a small square and then a large square by calling the forward method multiple times. In the next lessons, we will draw more complicated shapes. Here are some simple turtle methods that you could use:

  • yertle.forward();

  • yertle.turnLeft();

  • myrtle.turnRight();

  • myrtle.backward();

  • myrtle.penUp();

  • yertle.penDown();

After writing your code below, if you’d like your own copy, you can open this repl.it link, copy in your code, and save it in your own repl.it account.

2.1.4. Summary

  • A class defines a new data type (classify something). It is the formal implementation, or blueprint, of the attributes and behaviors of the objects of that class.

  • An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.

You have attempted of activities on this page
Next Section - 2.2. Creating and Storing Objects: Constructors