Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/02 Algorithms and programming/Programming Techniques/Use of object oriented techniques

Use of object oriented techniques

Use of object oriented techniques.

2.2.1 Programming Techniques

Programming Techniques — Note 6 of 6

Candidates need to understand classes, objects, attributes, methods, encapsulation (including get and set methods), inheritance and polymorphism. Candidates need to be able to read, trace and write object-oriented code, interpret class diagrams to produce class definitions, and derive an object-oriented solution for a given scenario.

What you need to know

Object-oriented techniques are used to design and write programs as collections of interacting objects. You need to read, trace and write OOP code, interpret class diagrams, and decide when an object-oriented approach suits a problem.

What to know

  • Classes define attributes and methods; objects are instances of classes created with a constructor.
  • Encapsulation: attributes are private and accessed through public getter and setter methods, so data can be validated and protected.
  • Inheritance: a subclass inherits from a superclass and can add or override attributes and methods, avoiding duplicated code.
  • Polymorphism: the same method call behaves differently depending on the object's class, e.g. each subclass overriding draw().
  • OOP suits problems with many interacting entities (e.g. games, simulations, user interfaces), and supports reuse and teamwork because classes can be developed and tested separately.

Key terms

Exam tipCommon mistake

Class diagram

A diagram showing each class's name, attributes and methods (with + for public and - for private), and arrows showing inheritance.

  • Exam tip: You may need to turn a class diagram into a class definition, or derive an object-oriented solution for a scenario.

Exam technique

  • Mistake:Accessing a private attribute directly from outside its class instead of using a getter or setter.

Code examples

Encapsulation, inheritance and polymorphism in Python
python
class Shape:
    def __init__(self, name):
        self.__name = name           # private attribute

    def get_name(self):              # getter
        return self.__name

    def area(self):
        return 0

class Rectangle(Shape):             # inheritance
    def __init__(self, width, height):
        super().__init__("Rectangle")
        self.width = width
        self.height = height

    def area(self):                  # overrides Shape.area (polymorphism)
        return self.width * self.height

shapes = [Shape("Blank"), Rectangle(3, 4)]
for s in shapes:
    print(s.get_name(), s.area())    # Blank 0, then Rectangle 12

Programming Techniques

Note 6 of 6

  1. 1Programming constructs: sequence, iteration, branching
  2. 2Recursion, how it can be used and how it compares to an iterative approach
  3. 3Global and local variables
  4. 4Modularity, functions and procedures, parameter passing by value and by reference
  5. 5Use of an IDE to develop/debug a program
  6. 6Use of object oriented techniques
Topic overview
← Previous noteUse of an IDE to develop/debug a program2.2.1 Programming TechniquesNext topic →Features that make a problem solvable by computational methods2.2.2 Computational Methods

Back to Programming Techniques