Classes, objects, attributes, methods, constructors, encapsulation, inheritance and polymorphism, as OCR A Level H446 (topics 1.2.4 and 2.2.1) examines them. Build objects from a bank account class hierarchy, call their methods and watch what runs.
A blueprint or template. It defines the attributes (data) and methods (procedures and functions) that every object of that type will have. No data is stored until an object is made.
An instance of a class, created by new (instantiation). Each object has its own copy of the attributes, so two accounts can hold different balances while sharing the same methods.
Attributes are made private so they can only be read or changed through the class's public methods (getters and setters), which can validate the change.
A subclass inherits the attributes and methods of its parent (superclass) and adds or changes some. super.new() runs the parent's constructor.
Objects of different classes respond to the same method call in their own way, because a subclass can override a method. Code can treat them all as the parent type.
class Account names the blueprint. Nothing exists in memory yet.new is the constructor: it runs once, when an object is instantiated, and sets up the attributes.Choose what to create, then step through what new does.
new creates an object. In OCR pseudocode it is called new; in Python it is __init__.super.new(...) first so the parent's attributes are set up, then sets its own.= stores a reference to the new object. Two variables can refer to the same object; two new calls make two separate objects.A small class with one public attribute and one private attribute, so you can see the difference when you try to use them from outside.
__score) mark an attribute as private. Python does not enforce it as strictly as other languages, but the exam treats it as private.Class diagram notation: the name on top, then attributes, then methods. − means private and + means public. The hollow-headed arrow points from the subclass to the class it inherits from.
SavingsAccount can deposit even though it never defines it.interestRate, addInterest).describe, canWithdraw). The subclass version replaces the parent's for objects of the subclass.super.new(...) and super.describe() call the parent's version from inside the subclass.getBalance(). That is encapsulation applying to subclasses too.When a method is called on an object, the program looks in the object's own class first, then its parent, and so on up the chain.
The loop below does not know or care what kind of account each element is. It calls the same method on each one; each object runs its own class's version.
Account) can hold objects of any subclass, so one loop handles them all.Auto-marked. Terms are single words such as class, object, constructor, encapsulation.
new. "Account is a class; acc1 is an object (an instance of Account)." Never say the class stores the balance.new in OCR pseudocode: public procedure new(...). In Python it is def __init__(self, ...). Do not mix the two.acc1 = new Account("Ava", 100). The arguments must match the constructor's parameters in number and order.acc1.deposit(50), with the object name, a dot, the method and brackets. A function call returns a value, so use it in an expression or print: print(acc1.getBalance()).class SavingsAccount inherits Account, and super.new(...) as the first line of the subclass constructor. Python: class SavingsAccount(Account): and super().__init__(...).