Loading...
A class defines object state and behaviour. Trace separate instances, controlled access to attributes and overridden methods using OCR’s constructor and inheritance conventions.
What you'll be able to do
A class defines attributes and methods; an object is an instance with its own relevant state. Two Ticket objects can have different remaining capacities even though they share the same class definition. A constructor initialises a new object, and OCR denotes this with a procedure named new. Creating an instance uses new ClassName(arguments).
Methods and attributes are assumed public unless the code specifies access. A private attribute should be accessed through the class’s permitted behaviour rather than directly from arbitrary client code. Encapsulation supports invariants: the reserve method below changes remaining only when the requested amount is positive and does not exceed availability. The constructor’s input is assumed non-negative.
A subclass can inherit state and behaviour from a superclass and supply specialised behaviour. OCR uses inherits in the class definition and super to call superclass methods, including super.new(...) for constructor work. The ReducedTicket class reuses the capacity management but overrides price to return a different value.
Polymorphism allows the same method call, such as ticket.price(), to select behaviour appropriate to the actual object. It does not mean that every inherited attribute automatically changes type or value. All methods here are instance methods, matching OCR’s guide; static methods are not needed for this convention. A class diagram can be translated into attributes, method signatures and inheritance relationships before implementation.
Create Ticket(3), reserve two places and its remaining count becomes one. A separate ReducedTicket(3) still has three unless it is also changed. Calling their price methods returns 10 and 6 respectively. The same method name has different overridden implementations, while both objects follow the reservation interface.
An object design suits situations where state and related operations form useful units. It is not automatically superior for every short calculation. Choose responsibilities carefully so that callers do not need to manipulate many internal fields to complete one operation. Test valid and invalid requests, separate-instance state and behaviour through the common interface. In independent coursework, derive the actual classes from the learner’s own problem and requirements.
class Ticket
private remaining
public procedure new(capacity)
remaining = capacity
endprocedure
public function reserve(amount)
if amount > 0 AND amount <= remaining then
remaining = remaining - amount
return true
endif
return false
endfunction
public function getRemaining()
return remaining
endfunction
public function price()
return 10
endfunction
endclass
class ReducedTicket inherits Ticket
public procedure new(capacity)
super.new(capacity)
endprocedure
public function price()
return 6
endfunction
endclass
standard = new Ticket(3)
reduced = new ReducedTicket(3)
print(standard.reserve(2))
print(standard.getRemaining())
print(reduced.getRemaining())
print(standard.price())
print(reduced.price())Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.