Object-oriented programming
| English | Chinese | Pinyin |
|---|---|---|
| object | 对象 | duì xiàng |
| encapsulation | 封装 | fēng zhuāng |
| class | 类 | lèi |
| instance | 实例 | shí lì |
| constructor | 构造函数 | gòu zào hán shù |
| inheritance | 继承 | jì chéng |
| subclass | 子类 | zi lèi |
| superclass | 父类 | fù lèi |
| polymorphism | 多态 | duō tài |
| abstraction | 抽象 | chōu xiàng |
A change in one place that used to mean a change in fifty
- A program stores a customer's balance as a public number. Twenty routines read it and fifteen write it. Now the balance must never go negative.
- Without objects, that rule has to be added at fifteen places, and the day someone adds a sixteenth writer it is broken again.
- With objects, the balance is private and the only way to change it is a method that enforces the rule. There is one place to add it and no way round it.
- That is encapsulation 封装, and it is the reason object-oriented programming exists. This lesson is objects, classes 类, and the four pillars.
Objects and classes
- An object 对象 combines attributes, its data, with methods, the operations that act on that data. It is a unit of state and behaviour together.
- A class is the blueprint: it defines what attributes and methods every object of that kind will have. An object is an instance 实例 of a class, with its own values.
- One
Taxiclass, and each actual taxi is an object with its own registration and capacity.

Attributes above, methods below, and their visibility marked
Programming concept lab
Connect examples to the programming idea they show.
An object combines:
An object bundles its data (attributes) with the operations (methods) that act on it.
What is the relationship between a class and an object?
One Taxi class defines the attributes and methods; each actual taxi is an object with its own capacity and registration.
Constructors, getters and setters
- A constructor 构造函数 is a special method that runs when an object is created, to set up its attributes to valid initial values.
- Because attributes are private, a class provides get methods to read a value and set methods to change one. A set method is where a rule such as "never negative" is enforced.
- That is what makes the private attribute worth having: the rule is enforced on every change, because there is no other route to the data.
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC PROCEDURE NEW(StartCapacity : INTEGER)
Capacity ← StartCapacity
ENDPROCEDURE
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS
A constructor is a special method that:
The constructor initialises a new object's attributes at creation time.
Pillar 1: encapsulation
- Encapsulation means an object's data is hidden behind its methods. Attributes are private; outside code uses the public methods.
- Two benefits, and the exam wants both: the object can enforce its own rules on every change, and the internal representation can be changed safely later, because no outside code depends on it.
- Store the balance in pounds today and in pennies tomorrow: every user of the class still calls
GetBalance()and nothing outside breaks.
Encapsulation means:
Encapsulation hides the data; outside code uses the public methods, so internals can change safely.
What does encapsulation buy? Select all that apply.
Encapsulation is about maintainability and correctness, not speed. Private data with public methods is what makes both possible.
Pillar 2: inheritance
- Inheritance 继承 lets a subclass 子类 be defined in terms of a superclass 父类, taking all its attributes and methods and then adding its own or overriding them.
- It models an "is-a" relationship: a Manager is an Employee, so
Managerinherits fromEmployeeand adds a department. - The benefit is that shared code is written once in the superclass, and a change to it reaches every subclass.

The shared part lives above; the differences live below
Inheritance models an "is-a" relationship (a Manager is an Employee), so a subclass inherits and extends its superclass.
Inheritance is "is-a" specialisation; "has-a" relationships are modelled by composition instead.
Inheritance models an "____ a" relationship, such as a Manager being an Employee.
A car has an engine, which is composition rather than inheritance. Getting that distinction right is what the question tests.
Pillars 3 and 4: polymorphism and abstraction
- Polymorphism 多态 means different objects respond to the same method call in their own way. Every
Shapehas anArea()method;CircleandRectangleeach implement it differently. - Its value: code can loop over a list of shapes calling
Area()on each, without knowing or caring which kind each one is. Add aTriangleclass later and that loop needs no change at all. - Abstraction 抽象 means showing a simple interface and hiding the complexity behind it: the caller of
Area()need not know any geometry.
Polymorphism allows:
The same call (e.g. Area()) behaves correctly for each type — the caller need not know the exact class.
Put the consequences of polymorphism in order, from the definition to the benefit.
One call, several implementations, and the code that uses it never has to know about the new ones.
Worked example: name the pillar
- A
BankAccountclass keeps its balance private and providesDeposit()andWithdraw()methods that refuse to make it negative. Encapsulation: the data is hidden and the rule is enforced by the only routes to it. - A
SavingsAccountclass is defined as aBankAccountwith an added interest rate. Inheritance: an is-a relationship, reusing the superclass's code. - A loop calls
Print()on every document in a list, and each type prints itself differently. Polymorphism: one call, several implementations. - Name the pillar and the feature of the scenario that shows it.
Match each OOP pillar to its meaning.
Encapsulation = hiding; inheritance = is-a specialisation; polymorphism = same interface, varied behaviour.
Worked example: why OOP for a large system
- Give two advantages of using object-oriented programming for a large system. [4]
- Encapsulation means each class's internals can be changed without affecting the rest of the program, so the system is easier to maintain and a team can work on classes in parallel.
- Inheritance means shared behaviour is written once and reused, so there is less duplicated code and a fix applies everywhere at once.
- Polymorphism means new kinds can be added without changing the code that uses them. Each answer is a feature and its consequence.
Marks that slip away
- A class is the blueprint, an object is an instance of it. Do not use the words interchangeably.
- Encapsulation is hiding data behind methods, and its benefits are rule enforcement and safe internal change. "It is more secure" is too vague.
- Inheritance is an is-a relationship. A car has a engine, which is composition, not inheritance.
- Polymorphism is the same call behaving differently per class, which is what lets a new subclass be added without changing existing code.
You've got it
- an object combines attributes and methods; a class is the blueprint and an object is an instance, set up by a constructor
- encapsulation: private data behind public methods, so rules are enforced on every change and internals can change safely
- inheritance: a subclass takes a superclass's members and adds or overrides them, modelling is-a and writing shared code once
- polymorphism: the same call implemented differently per class, so new kinds need no change to existing code; abstraction: a simple interface hiding the complexity