OOP Flashcards
Week 1.7 (14 cards)
objects
instances of a class
class
objects characterised by the same attributes & behaviour
3 special methods
- __init__
- __str__ & __repr
- __len__
__init__
constructor method
__str__
- str() on an object
- more readable & user-friendly
- targeted for end-users
__repr__
- repr() on an object
- more detailed
- used for debugging & logging
- targeted for developers
what are the 2 types of instance relationships
- association
- comparision
association relationship
for general relationship describing when one class uses the functionalities provided by another class
composition relationship
for the relationship between a whole thing & its component parts
why do we use inheritance
- 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
when should we not use inheritance
just as a way of sharing fields & methods between classes
syntax for inheritance
class subClass(SuperClass)
superclass vs subclass
- 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
why doesn’t comparing objects usually work
objects with identical values are not considered equal by default as they are not the same object and have different memory addresses