A-level Computer Science revision · Programming

Classes and objects

The idea

In object-oriented programming, a class is a template defining attributes (data) and methods (behaviour). An object is an instance of a class. A constructor method creates and initialises a new object. Each object has its own attribute values.

  1. Class: blueprint. Object: instance.
  2. Attributes store state; methods define behaviour.
  3. Use the constructor (new) to create objects.

Watch out Each object holds its own values for its instance attributes; only class (static) attributes are shared.

See it worked

Worked example

Dog's constructor sets the name attribute to its argument. What happens when myDog = new Dog('Rex') runs?

  1. The Dog constructor is called.
  2. A new object is created.
  3. Its name attribute is set to 'Rex'.

Answer A new Dog object is created with name 'Rex'

Your turn

Question 11 mark

What is an object?

Choose one
Question 21 mark

What is the purpose of a constructor?

Choose one
Question 32 marks

Explain the difference between an attribute and a method.

Write your answer, then see a model answer
  1. Define attribute.
  2. Define method.

Answer An attribute is a variable that stores data about an object; a method is a subroutine that defines what the object can do.

A-level Computer Science Pro, OCR H446

A-level Computer Science Pro

£119 once

Price rises to £199 on 1 January. Save £80 by joining before then.

At least 12 months of access

By paying you agree to our Terms and get access straight away, which ends your 14-day cancellation right.

No VAT is charged.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms