Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/01 Computer systems/Types of Programming Language/Object-oriented languages

Object-oriented languages: classes, objects, methods, attributes, inheritance, encapsulation, polymorphism

Object-oriented languages (see appendix 5d for pseudocode style) with an understanding of classes, objects, methods, attributes, inheritance, encapsulation and polymorphism

1.2.4 Types of Programming Language

Practise exam questions

Types of Programming Language — Note 5 of 5

Candidates need to understand object-oriented code (as specified in the pseudocode guide). They need to have an understanding of classes, objects, attributes and methods. They need to understand the difference between private and public attributes and methods. Candidates need to understand encapsulation and the use of get and set methods to access private attributes. Candidates need to understand the purpose and principles of inheritance. Candidates need to have an understanding of polymorphism and how it can be used within a program. Candidates need to be able to read, trace, amend and write code that makes use of these object-oriented techniques

What you need to know

Object-oriented programming (OOP) models a system as a collection of objects that combine data and the methods that act on it. You need to understand the key OOP concepts and be able to read, trace, amend and write code using OCR's object-oriented pseudocode style.

What to know

  • Benefits of OOP: code is reusable through inheritance; encapsulation makes code more secure and easier to maintain; classes can be developed and tested independently.

Key terms

Class

A template or blueprint that defines the attributes and methods that objects of that type will have.

Object

An instance of a class, with its own values for the attributes.

Attribute

A variable belonging to a class/object that holds its data.

Method

A subroutine belonging to a class that defines the behaviour of an object.

Constructor

A special method that runs when an object is created to set its initial attribute values. In OCR pseudocode it is a procedure called new.

Encapsulation

Keeping an object's attributes private so they can only be accessed or changed through its public methods (getters and setters). This protects the data from accidental or invalid changes.

Inheritance

A subclass takes on the attributes and methods of a superclass, and can add its own. In OCR pseudocode: class Dog inherits Pet.

Polymorphism

Objects of different classes can respond to the same method call in different ways, e.g. a subclass overriding a method of its superclass.

Exam technique

  • Tip:In OCR pseudocode, super.new(...) calls the superclass constructor, and objects are created with objectName = new ClassName(parameters).
  • Mistake:Confusing a class with an object. The class is the template; the object is a specific instance created from it.

Code examples

OCR pseudocode: encapsulation, inheritance and polymorphism
text
class Animal
    private name
    public procedure new(givenName)
        name = givenName
    endprocedure
    public function getName()
        return name
    endfunction
    public function speak()
        return "..."
    endfunction
endclass

class Dog inherits Animal
    public procedure new(givenName)
        super.new(givenName)
    endprocedure
    public function speak()        // overrides Animal.speak (polymorphism)
        return "Woof"
    endfunction
endclass

myDog = new Dog("Rex")
print(myDog.getName() + " says " + myDog.speak())

Ready to test yourself?

Practise 1.2.4 questions
← Previous noteModes of addressing memory: immediate, direct, indirect and indexed1.2.4 Types of Programming LanguageNext topic →Lossy vs lossless compression1.3.1 Compression, Encryption and Hashing

Back to Types of Programming Language

Types of Programming Language

Note 5 of 5

  1. 1The need for and characteristics of a variety of programming paradigms
  2. 2Procedural languages
  3. 3Assembly language and the Little Man Computer instruction set
  4. 4Modes of addressing memory: immediate, direct, indirect and indexed
  5. 5Object-oriented languages: classes, objects, methods, attributes, inheritance, encapsulation, polymorphism
Topic overview