212 Classes and objects

  1. What is the role of a class in object-oriented programming?

Show solution

A class acts as a template or blueprint that defines what an object will look like and how it will behave when created. It specifies the structure and features shared by all objects of that class.

  1. How is an object related to a class?

Show solution

An object is an instance created from a class. While the class defines the shared structure and behaviour, each object can have its own attribute values but follows the design of the class.

  1. In the cupcake analogy, what represents the class and what represents the objects?

Show solution

The cupcake wrapper represents the class — it defines the shape and structure of the cupcakes. The individual cupcakes made using the wrapper represent the objects — they can have different flavours and decorations but share the same shape.

  1. Give an example from everyday life that shows the relationship between a class and an object.

Show solution

One example is a blueprint for a house (class) and the actual house built from that blueprint (object). The blueprint defines the design, while each house built can have its own colours or interior features.

  1. How do the attributes of objects created from the same class vary?

Show solution

Objects created from the same class can have different attribute values. For example, cupcakes made from the same wrapper (class) can have different flavours, frostings, and toppings, even though they share the same basic shape.

Last updated

Was this helpful?