A-level Computer Science revision · Programming

Encapsulation, inheritance and polymorphism

The idea

Encapsulation keeps attributes private and allows access only through methods (getters and setters), protecting data. Inheritance lets a subclass take the attributes and methods of a superclass and add its own. Polymorphism lets methods with the same name behave differently for different classes, for example by overriding.

  1. Encapsulation: private attributes, public methods.
  2. Inheritance: subclass extends superclass.
  3. Polymorphism: overriding gives class-specific behaviour.

Watch out Overriding redefines an inherited method in a subclass; it does not delete the superclass method.

See it worked

Worked example

Why use encapsulation?

  1. Attributes are private.
  2. They can only be changed through methods that validate input.
  3. This protects data and hides implementation.

Answer To protect data so it is changed only through controlled methods

Your turn

Question 11 mark

Class Car inherits from class Vehicle. Which is true?

Choose one
Question 21 mark

Shape.area() is redefined in Circle and Square to calculate each area. This is:

Choose one
Question 32 marks

Give one advantage of inheritance.

Write your answer, then see a model answer
  1. Name the advantage.
  2. Explain it.

Answer Common code is written once in the superclass and reused by subclasses, reducing duplication and making changes easier.

A-level Computer Science Pro, OCR H446

A-level Computer Science Pro

£119 once

Price rises to £199 on 1 January. Save £80 by joining before then.

At least 12 months of access

By paying you agree to our Terms and get access straight away, which ends your 14-day cancellation right.

No VAT is charged.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms