OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Problem Solving and Programming
Programming techniques: Use of object oriented techniques
- 1 thing to know
- Component 02
Overview
Recall questions on classes and objects, attributes and methods, constructors, separate object state, encapsulation with private attributes and get and set methods, inheritance and superclass constructors, polymorphism, and when object-oriented design fits.
What you need to know
Answer each one before you reveal it.
Questions from this subtopic
Each one with the model answer underneath.
- 1
What is the difference between a class and an object?
Model answer
A class is a template or blueprint: it defines what attributes objects of that kind will have and what methods they can carry out, but it holds no data of its own. An object is an instance of a class, created while the program runs, with its own copy of the attributes. Dog is a class; the particular dog created by myDog = new Dog("Fido", "Scottish Terrier") is an object. One class can have any number of objects, each holding different attribute values but all sharing the same methods and the same structure.
- 2
What are attributes and methods, and why are they kept together?
Model answer
An attribute is a variable belonging to an object that holds part of its state — a pet's name, an account's balance, a player's number of attempts. A method is a subroutine belonging to the class that defines the object's behaviour and normally works on that object's own attributes — deposit, getBalance, describe. They are kept together because that is the central idea of object-oriented programming: the data and the operations permitted on that data live in one place, so the rules about the data cannot be bypassed and anyone maintaining the code knows where to look.
- 3
What is a constructor and when does it run?
Model answer
A constructor is a special method that runs automatically at the instant an object is created, and it is given the arguments supplied in the creation statement. Its job is to set the new object's attributes to sensible starting values. Writing myDog = new Dog("Fido", "Scottish Terrier") reserves memory for a new Dog object and immediately runs its constructor with those two values, which store them into the object's attributes. Without it, a new object would begin life with empty attributes and every piece of calling code would have to remember to fill them in.
- 4
What does this print, and what does the answer show about objects? class Account private balance public procedure new(startBalance) balance = startBalance endprocedure public procedure deposit(amount) balance = balance + amount endprocedure public function getBalance() return balance endfunction endclass a = new Account(100) b = new Account(100) a.deposit(50) print(a.getBalance()) print(b.getBalance())
Model answer
It prints 150 then 100. Each object created from the class gets its own separate copy of the attribute balance, so depositing into a has no effect whatever on b, even though both were created the same way and both run the same deposit method. The methods are shared by the class; the data is per object. This is what makes objects useful for modelling many similar things — a thousand accounts, all behaving identically, each with its own state.
- 5
What is encapsulation, and what two practical problems does it solve?
Model answer
Encapsulation means keeping an object's attributes private and allowing the outside world to reach them only through the class's public methods. It solves validation: a set method can check a value and refuse an impossible one — a method setting the number of attempts can reject a negative number — whereas a public attribute could be assigned anything by any code anywhere. And it solves maintainability: because no outside code depends on how the data is actually stored, the class's internals can be changed later, for instance storing a date as three numbers instead of one string, and every other part of the program keeps working provided the public methods still behave the same.
- 6
If a class keeps an attribute private, how does code outside the class get at it?
Model answer
Through public methods that the class chooses to provide: a get method that returns the value and a set method that changes it after making whatever checks the class wants. Outside code calls them on the object, for example player.setAttempts(5) and print(player.getAttempts()). The class controls exactly what is exposed, so it can offer a getter with no matching setter to make an attribute readable but not changeable from outside, or provide a method that changes the value in a controlled way, such as withdraw, rather than exposing the balance for direct assignment.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Problem Solving and Programming, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.