Skip to content
ExamSpine

Lesson 8.1

Class versus object; making an instance

Part 8: One classReading, about 6 min2 exercises in this lesson

Class vs object in Python and Java: making an instance

By the end of this you will be able to say how a class differs from an object, and make objects from a class in Python and in Java.

A parcel company sends thousands of parcels. Each parcel has a town it is going to and a weight in grams. Two variables for every parcel would be hopeless. Instead, the program says once what a parcel is, and then makes as many parcels as it needs.

One class, two objects

Here is a program that does that. Parcel is the class. a and b are two parcels made from it.

class Parcel:
    pass

a = Parcel()
a.town = "Leeds"
a.weight = 500
b = Parcel()
b.town = "Hilo"
b.weight = 1200
a.weight = a.weight + 100
print(a.town + " " + str(a.weight))
print(b.town + " " + str(b.weight))
class Parcel {
    String town;
    int weight;
}

public class Main {
    public static void main(String[] args) {
        Parcel a = new Parcel();
        a.town = "Leeds";
        a.weight = 500;
        Parcel b = new Parcel();
        b.town = "Hilo";
        b.weight = 1200;
        a.weight = a.weight + 100;
        System.out.println(
            a.town + " " + a.weight);
        System.out.println(
            b.town + " " + b.weight);
    }
}

When it runs:

Leeds 600
Hilo 1200

Here is what each parcel holds as the program runs:

AfterParcel aParcel b
a's town and weight are setLeeds, 500not made yet
b's town and weight are setLeeds, 500Hilo, 1200
a.weight = a.weight + 100Leeds, 600Hilo, 1200

The line a.weight = a.weight + 100 changed a and nothing else. Each object keeps its own town and its own weight, so changing one parcel never changes another.

Class or object

A class is a description of one kind of thing. In this program, every parcel has a town and a weight. In Java, the class Parcel lists them. In Python, this first Parcel is still empty, and the program gives each parcel its town and weight; lesson 8.4 moves them into the class. Either way, the class is not a parcel itself, and it holds no town of its own.

An object is one thing made from that description, with its own values. a is one parcel, going to Leeds. b is another, going to Hilo. One class can make as many objects as the program needs.

Think of a blank label that a courier fills in. The blank label is the class: it says which boxes every label has. Each filled-in label, stuck on one parcel, is an object.

Making an object

Making a new object from a class is called instantiation, and the new object is an instance of the class. a is an instance of Parcel.

  • In Java, the word new makes the object. Parcel a = new Parcel(); makes a new parcel and puts it in a variable called a, whose type is Parcel. A class is a type, just as int is.
  • In Python, you call the class, with brackets after its name, as you would call a function. a = Parcel() makes a new parcel and puts it in a. Python has no new.

Each new Parcel() in Java, or each Parcel() in Python, makes one more separate object. The program above instantiates Parcel twice, so there are two parcels.

Attributes and the dot

A value an object keeps, such as its town, is an attribute. A dot reaches inside one object: a.weight is the weight of parcel a. So a.weight = a.weight + 100 reads a's weight, adds 100, and stores the answer back in a.

The two languages set the attributes up differently in this lesson.

  • In Java, the class lists each attribute with its type: String town; and int weight;. A new parcel has them straight away, with a weight of 0 until the program sets it.
  • In Python, this class is empty. Python needs at least one line inside a class, and pass is a line that does nothing. The program gives each object its attributes by assigning to them, as in a.town = "Leeds". Reading an attribute that was never set stops the program with an error.

Both ways leave it to you to set every attribute by hand, which is easy to forget. Lesson 8.4 shows the usual way: a constructor in Java, or __init__ in Python, which sets the attributes the moment the object is made.

Two common slips

  • In Java, leaving out new. Parcel a = Parcel(); does not compile, because Java reads Parcel() as a call to a method that does not exist.
  • In Python, leaving out the brackets. a = Parcel makes no object at all: it only gives the class a second name. No error appears: a.town = "Leeds" would then set a town on the class itself, and every parcel made later would share it.

Now try it yourself on the questions below.

Exercise 8.1.18.1.1 · Answer itClass or object

A cinema has 120 seats. Its booking program has one class called Seat, which says that every seat has a row letter and a seat number. When the program starts, it makes one Seat object for each seat in the cinema.

(a) Distinguish between the class Seat and one Seat object, using the cinema as your example. [2]

(b) State how many times the program instantiates Seat when it starts. [1]

(c) The program changes the row letter of one Seat object. State what happens to the row letters of the other Seat objects. [1]

[4]

Exercise 8.1.2 · Trace itTrace: two objects

Complete the trace table for this Python code. Jar is a class. a and b are objects made from it, and sweets is an attribute that each of them keeps. Then answer the question below the table. [3]

Line 1: class Jar:
Line 2, indented 1 level: pass
Line 3:
Line 4: a = Jar()
Line 5: a.sweets = 4
Line 6: b = Jar()
Line 7: b.sweets = 9
Line 8: a.sweets = a.sweets + 3
Line 9: b.sweets = b.sweets - a.sweets
Line 10: x = a.sweets
Line 11: a.sweets = 0
Line 12: y = b.sweets + a.sweets
Line 13: print(x + y)

Fill in one row each time a line changes x or y, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowxyoutput
1
2
3

Exercise 8.1.2 · Trace itTrace: two objects (Java)

Complete the trace table for this Java code. Jar is a class. a and b are objects made from it, and sweets is an attribute that each of them keeps. Then answer the question below the table. [3]

Line 1: class Jar {
Line 2, indented 1 level: int sweets;
Line 3: }
Line 4:
Line 5: public class Main {
Line 6, indented 1 level: public static void main(String[] args) {
Line 7, indented 2 levels: Jar a = new Jar();
Line 8, indented 2 levels: a.sweets = 4;
Line 9, indented 2 levels: Jar b = new Jar();
Line 10, indented 2 levels: b.sweets = 9;
Line 11, indented 2 levels: a.sweets = a.sweets + 3;
Line 12, indented 2 levels: b.sweets = b.sweets - a.sweets;
Line 13, indented 2 levels: int x = a.sweets;
Line 14, indented 2 levels: a.sweets = 0;
Line 15, indented 2 levels: int y = b.sweets + a.sweets;
Line 16, indented 2 levels: System.out.println(x + y);
Line 17, indented 1 level: }
Line 18: }

Fill in one row each time a line changes x or y, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Java shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowxyoutput
1
2
3