OOP Flashcards
Week 1.7 (14 cards)
1
Q
objects
A
instances of a class
2
Q
class
A
objects characterised by the same attributes & behaviour
3
Q
3 special methods
A
- __init__
- __str__ & __repr
- __len__
4
Q
__init__
A
constructor method
5
Q
__str__
A
- str() on an object
- more readable & user-friendly
- targeted for end-users
6
Q
__repr__
A
- repr() on an object
- more detailed
- used for debugging & logging
- targeted for developers
7
Q
what are the 2 types of instance relationships
A
- association
- comparision
8
Q
association relationship
A
for general relationship describing when one class uses the functionalities provided by another class
9
Q
composition relationship
A
for the relationship between a whole thing & its component parts
10
Q
why do we use inheritance
A
- model an ‘is a kind of’ relationship
- subclasses can add more fields & methods or make changes to existing methods in the parentclass to extend behaviour
11
Q
when should we not use inheritance
A
just as a way of sharing fields & methods between classes
12
Q
syntax for inheritance
A
class subClass(SuperClass)
13
Q
superclass vs subclass
A
- subclasses can override methods from superclass to provide specific functionality
- when a method with the same name were defined in both superclass & a subclass, use super() to refer to method in superclass
- using super to reuse initialisation and other superclass’ methods helps avoid repeating code across classes
14
Q
A