OOP and other coding info Flashcards
(24 cards)
what is a class
A blueprint for creating objects.
It defines a set of attributes (data) and methods (functions) that the objects created from the class will have.
what is an object
Objects are instances of a class created with specifically defined data
examples of classes and objects
Class: Car
Attributes: color, model, year
Methods: drive(), brake()
Object: my_car (an instance of Car)
Attributes: color = ‘red’, model = ‘Toyota’, year = 2022
Methods: my_car.drive(), my_car.brake()
what is a method
a block of code associated with a class or object, defining the behavior and actions an object can perform
- or simply put, its a function that goes inside a class
What is Object-Oriented Programming (OOP)?
A programming paradigm based on the concept of objects, which can contain data (attributes) and code (methods).
What is a constructor in Python?
A special method called __init__() used to initialise an object’s attributes when it is created.
What is instantiation?
The process of creating an object from a class using the class name and parentheses (e.g. player = Player()).
What is encapsulation?
The concept of hiding the internal state of an object and only allowing access through public methods.
What is a private attribute in Python?
An attribute prefixed with double underscores (e.g. __score) to signal it should not be accessed directly from outside the class.
: What is inheritance?
A mechanism where a child class inherits the attributes and methods of a parent class, allowing for reuse and extension.
What is super() used for?
To call methods from the parent class, typically used in a child class’s constructor.
refers to the class we have inherited from
What is polymorphism?
The ability for different classes to have methods with the same name but potentially different behaviours.
What is method overriding?
When a child class provides a new version of a method that is already defined in the parent class.
What is a getter and setter?
Methods used to access (get) and update (set) the value of a private attribute.
how does a class attribute differ from a regular attribute
an attribute that doesnt use self, not specific to any instance
difference between variables and constants
advantages of named constants
A variable is a storage location in memory that can hold a value, and the value can change during the program’s execution.
A constant is also a storage location, but its value is fixed and cannot be changed after initialization.
Advantages of using named constants:
Readability:
Constants are given meaningful names, making the code easier to understand.
Maintainability:
If the value of a constant needs to be changed, it only needs to be modified in one place, not throughout the code where the literal value is used.
Code clarity:
Constants help separate data values from the logic of the program, improving code clarity.
Efficiency:
Using named constants can sometimes improve performance, especially when dealing with complex operations.
describe the function of parameters
used to pass data between different parts of a program, like functions or subroutines, allowing them to share and manipulate values.
This avoids the need for global variables, reducing the risk of unintended side effects and making code more organized.
local vs global variables
local variables are defined within a function or block of code and are only accessible from within that scope
. Global variables, on the other hand, are declared outside any function and are accessible throughout the entire program
Advantages of the structured approach / using hierarchy charts
Advantage Explanation
Modular design Each module is small and manageable.
Easier to test/debug Can test individual modules independently.
Reusability Modules can be reused in other programs.
Improved readability Clear top-down flow of logic.
Teamwork-friendly Developers can work on different modules in parallel.
Reduced complexity Breaking down a large problem makes the system easier to understand and maintain.
Instantiation
The process of creating an object from a class.
Encapsulation
Hiding internal data of an object and only allowing access through public methods.
Aggregation
A “has-a” relationship: one object uses another, but they can exist independently. UML: ⚪ (white diamond).
Overriding
When a subclass provides a new version of a method defined in the parent class.
Why Use OOP?
Easier modularity: break the system into manageable pieces.
Encourages code reuse through inheritance and composition.
Facilitates maintenance and updates.
Allows real-world modelling using objects.