Skip to main content
ScotlandComputer ScienceSyllabus dot point

How do classes, inheritance and polymorphism let one set of code model many related kinds of object?

Describe and implement object-oriented programming using classes, objects, instantiation, attributes and methods, encapsulation, inheritance, and polymorphism through method overriding.

A focused answer to the SQA Advanced Higher Computing Science content on object-oriented programming, covering classes and objects, instantiation, encapsulation, inheritance and polymorphism through method overriding, with UML class diagrams.

Generated by Claude Opus 4.813 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Classes, objects and instantiation
  3. Encapsulation
  4. Inheritance
  5. Polymorphism and method overriding
  6. Reading a UML class diagram
  7. Try this

What this dot point is asking

The SQA wants you to read, explain and write object-oriented (OO) code. You must know what a class and an object are, how an object is created, and the three pillars of OO: encapsulation, inheritance and polymorphism. You also need to read a UML class diagram.

Classes, objects and instantiation

A class defines the structure (attributes) and behaviour (methods) shared by a family of objects. Creating an object from a class is called instantiation, and the new object is an instance. Each instance has its own copy of the attribute values but shares the method definitions. A constructor is the special method that runs at instantiation to set up the object's initial state.

Encapsulation

Encapsulation bundles an object's attributes and methods inside the class and restricts direct access to the attributes. They are usually marked private and reached only through public methods (accessors and mutators, sometimes called getters and setters). This protects the data from being set to invalid values and lets the internal representation change without breaking other code.

Inheritance

Inheritance lets a subclass extend a superclass. The subclass automatically has the superclass's attributes and methods and can add new ones or replace existing ones. It models an "is a" relationship: a SavingsAccount is a BankAccount. The benefit is reuse: shared behaviour is written once in the superclass.

Polymorphism and method overriding

Polymorphism means "many forms": the same method call behaves differently depending on the object it is called on. It is achieved by overriding, where a subclass defines a method with the same name and signature as the superclass version. When that method is called through a superclass reference, the version that runs is the one belonging to the object's actual subclass, decided at run time. This lets you treat a collection of different subclass objects uniformly while each behaves correctly.

Reading a UML class diagram

A UML class diagram shows each class as a box with three parts: the class name, the attributes, and the methods. A + marks public members and a - marks private members. A hollow arrow from a subclass to its superclass shows inheritance.

classDiagram class BankAccount { -balance +getBalance() +deposit(amount) } class SavingsAccount { -rate +addInterest() } BankAccount <|-- SavingsAccount

Try this

Q1. State what is meant by instantiating a class. [1 mark]

  • Cue. Creating an object (instance) from the class, usually by calling its constructor.

Q2. Give one benefit of inheritance. [1 mark]

  • Cue. Code reuse: shared attributes and methods are written once in the superclass and inherited by every subclass.

Exam-style practice questions

Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AH style: define OO terms3 marksExplain the terms encapsulation, inheritance and polymorphism in object-oriented programming.
Show worked answer →

Encapsulation: bundling an object's data (attributes) and the methods that act on them inside the class, and hiding the attributes so they are accessed only through methods (1 mark). Inheritance: a subclass automatically receives the attributes and methods of its superclass, so shared code is written once and reused (1 mark). Polymorphism: a method call on a superclass reference runs the version defined in the actual object's subclass, so one call behaves differently for different object types (1 mark). Markers reward the correct concept, not the exact wording.

AH style: overriding4 marksA superclass Shape has a method area() that returns 0. Subclasses Circle and Rectangle each define their own area(). Explain how method overriding and polymorphism allow a list of Shape objects to report the correct area for each.
Show worked answer →

Each subclass redefines area() with the same name and signature as the superclass version: this is method overriding (1 mark). When area() is called on an element of the list, the version that runs is the one belonging to the object's actual class (Circle or Rectangle), not the Shape version: this is polymorphism, resolved at run time (1 mark). So looping over the list and calling area() on each element returns the circle's pi r squared or the rectangle's length times breadth as appropriate (1 mark). The benefit is that new shapes can be added by writing a new subclass with its own area(), with no change to the loop: the code is extensible (1 mark).

Related dot points

Sources & how we know this