1/57
Looks like no tags are added yet.
What is object-oriented programming (OOP)?
A form of programming that creates code for classes of objects, allowing many objects to be created from a single code base, making development more modular and extensible.
Fill in the gap: OOP lets many objects be created from a single ______ , making development more modular and extensible.
code base
Name the five advantages of OOP.
Increased modularity, code reusability, encapsulation, scale and collaboration.
How does increased modularity benefit a project?
Objects can be created and modified independently of each other, which makes large code bases easier to manage.
How does OOP achieve code reusability?
A class, once written, can be imported into other projects and reused many times.
How does OOP allow projects to scale?
Modularity and reusability allow a project to grow in size while remaining maintainable.
How does OOP improve collaboration?
Modularity makes it easy to delegate different parts of a project to different team members, so more people can work on the same project.
Name the six disadvantages of OOP.
Learning curve, increased complexity, overuse, overhead, lack of optimisation and object-centric design limitations.
Why is the learning curve a disadvantage of OOP?
Concepts such as classes, objects and inheritance can be difficult for beginner programmers to understand.
Why can OOP be a disadvantage for small problems?
A procedural approach would suffice, so a purely OOP approach is unnecessarily complex and harder to debug and maintain.
What can happen if OOP is overused or forced onto a problem?
Deep inheritance hierarchies, leading to code that is difficult to understand and maintain.
Fill in the gap: OOP introduces additional ______ in time and space requirements compared with paradigms that have less abstraction.
overhead
Why does OOP show a lack of optimisation?
It focuses on abstractions that improve modularity, at the cost of organising code in ways that are efficient for the CPU.
Which type of application may suit lower-level paradigms better than OOP, and why?
Performance-critical applications, because OOP's abstraction costs efficiency.
What are the object-centric design limitations of OOP?
Some problem domains are better modelled with other paradigms, such as functional or procedural programming.
Term: Name the three rows of a UML class diagram and what each contains.
Row 1 is the class name, row 2 is the variables (with data types) and row 3 is the methods (with parameter signatures and return data type).
In a UML class diagram, what does a + sign mean and what does a - sign mean?
Where in a UML class diagram are the instance variables and their data types listed?
In the second row.
Where in a UML class diagram are the methods with their parameters and return type listed?
In the third row.
Define instantiation.
The line of code that declares a new object variable based on the template provided by a class, which then executes the constructor to initialise the object.
Define constructor.
A special method in a class that is automatically executed during instantiation, whose main task is to initialise the instance variables before the object can be used by other code.
What is the name of the constructor method in Python?
init()
What does the keyword self do in a class?
It refers to the current object instance of the class and must prefix every instance variable or method.
Fill in the gap: ______ must be listed as the first parameter of every method belonging to a class.
self
Define encapsulation.
Bundling data and the methods that manipulate that data together into a single object, hiding the implementation details from outside code.
Fill in the gap: Encapsulation hides the ______ details of an object from outside code.
implementation
Define access modifiers.
Mechanisms provided by a programming language to control the visibility of methods and variables within an object.
In Python, what is the default access level of variables and methods?
Public.
What does a single underscore prefix (e.g. _variable) indicate?
That it is for internal use only, within the class.
What does a double underscore prefix (e.g. __variable) do in Python?
It is name mangled: the interpreter renames the variable or method at runtime, making it harder to access from outside the class.
Why was name mangling created, and does it enforce strict access control?
It was created to avoid naming conflicts, not to enforce strict access control.
Define accessor and give its other name.
A public method that lets external code access the value of a private instance variable, also known as a getter method.
Define mutator and give its other name.
A public method that lets external code update the value of a private instance variable, also known as a setter method.
Accessor or mutator: a method that sets the value of a private instance variable.
Mutator (setter).
Define static variables and methods.
Variables and methods that belong to the class rather than individual objects; only one copy is created and it is shared by all instances.
Define non-static variables and methods.
The normal instance variables and methods, where each object creates its own unique copy.
When are static variables and methods created?
At runtime by the interpreter or compiler when the class is defined, so no instance is needed for them to exist.
What happens when one object changes a non-static variable?
Nothing happens to other objects, because each object has its own copy of the variable.
Give a common use of a static variable.
Tracking ID values so that each object has its own unique ID number.
Fill in the gap: ______ variables belong to the class, while ______ variables belong to each individual object.
static, non-static
Define inheritance.
Where a class takes a copy of an existing class as the starting point for its methods and variables, which can then be overridden and extended to add functionality.
What is the main aim of inheritance?
To ease code reuse in complex projects, so only the custom modifications need to be written.
What are the two other names for a derived class?
Child class or subclass.
What are the two other names for the origin class?
Parent class or superclass.
How is an inheritance relationship invoked in Python?
By putting the parent class in parentheses in the class definition, e.g. class Customer(Person).
What must be done with the superclass constructor in a subclass, and where?
It must still be called, as the first line of the subclass constructor, using super().init()
Define polymorphism.
Meaning 'many forms': objects exhibit different behaviours based on their specific class implementation while still adhering to a shared interface or contract.
Define overriding.
When a child class creates a property or method with the same name as one in the parent class, thereby replacing it.
Give a GUI example of inheritance combined with polymorphic overriding.
Core text input box functionality is written once, then inherited and overridden to create more specialised input types.
Give a game design example of inheritance combined with polymorphic overriding.
A generic bot with a random move strategy is inherited by specialised bots, which override the strategy or abilities function.
What does overriding eq(self, other) allow?
Custom logic for the == operator when comparing two objects.
What does hash(self) do and what must be true of equal objects?
It returns an integer hash value for the object, and objects considered equal must have the same hash value.
Which methods are overridden for the comparison operators
lt, le, gt and ge.
What do getitem, setitem and delitem do?
They are called to retrieve, set or delete an item using the indexing syntax obj[key].
Which two methods make an object iterable?
iter(self) and next(self).
Define abstract class.
A generic template with no executable code of its own that cannot be instantiated and is designed to be extended by other classes.
When should you use an abstract class?
When all subclasses should have certain methods or fields but the implementation is specific to each subclass, so a default implementation makes no logical sense.
What is the benefit of an abstract class?
It provides standardisation and consistency, so users know certain methods are always available and new derived classes must adhere to the defined contract.