Object Orientated Programming Flashcards

(21 cards)

1
Q

What are classes?

A
  • Classes are used as blueprints or templates that can be used to create objects in OOP
  • This method allows for reusable and organised code in a modular way
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are objects?

A
  • An object is a representation of a real world entity
  • A class is a blueprint that describes the properties and behaviours of objects, while an object is a specific instance created using the blueprint
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are constructors?

A
  • A special method within a class that is automatically called when an object of that class is instantiated (created)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is a class set up called ItemForSale with 3 attributes: itemName, price, discount

A

class ItemForSale():
def __init__ (self, itemName, price, discount):
self.__itemName = itemName
self.__price = price
self.__discount = discount

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to add a new object to a class ItemForSale with 3 attributes

A

tank = ItemForSale(“tank”, 10, 10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is a method and what are the 2 different types?

A
  • A method is a function that is associated with objects or classes that define the behaviour and actions that an object can perform
  • The two types are functions and procedures
  • Functions: performs a task and returns a value
  • Procedure: performs a task but doesn’t return a value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe a public method

A
  • Accessible and can be invoked by any code within the same class or from any external classes
  • Changes to public methods may effect other parts of the code
  • Used when you want to provide accessibility of certain functionalities of an object or class to other parts of your program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe a private method

A
  • Only accessed within the same class and cannot be invoked by external code or other classes
  • Changes to private methods have a localised effect
  • Used when you have internal implementation details that should not be accessed or used by external code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an attribute?

A
  • Refers to a data memory or a property associated with an object or class
  • They define the state of an object and can have different values for different instances of the same class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is inheritence?

A
  • Allows a class to inherit the properties and behaviours of another class
  • Allows a derived class to inherit and utilise existing code from the base class
  • The derived class “is a” base class
  • E.G: the base class is vehicle and derived class is car. A car “is a” vehicle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is encapsulation?

A
  • The practice of grouping data (attributes) and methods (functions) within a class
  • Using encapsulation ensures that data remains secure and is not accidentally modified or misused by controlling access to them using access modifiers (public, private)
  • It also helps to organise code by keeping related methods and data together
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is abstraction in encapsulation?

A
  • Reduces complexity by hiding the implementation details of the object, making it easier to work with and understand
  • Programmers can use methods and classes from other parts of the program without having to understand how they are constructed internally
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is polymorphism?

A
  • Allows objects to take on different forms or behaviours
  • Different objects can share the same name or behaviour but can work in different ways
  • It helps make code more flexible, reusable and easier to manage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are get and set methods?

A
  • A get method returns attributes of a specific object
  • A set method changes the attributes of a specific object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would you code a get method for an ItemForSale?

A

class ItemForSale():
def __init__(self, itemName, price, discount):
self.__itemName = itemName
self.__price = price
self.__discount = discount

def getPrice(self):
     return self.\_\_price

tank.getPrice()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What keyword is used for inheritance?

A
  • Use the super(). keyword
17
Q

How would you code inheritance for an ItemForSale?

A

class tank(ItemForSale):
def __init__(self, itemName, price, discount, quantity):
super().__init__(itemName, price, discount)
self.__quantity = quantity

18
Q

What is encapsulation in terms of get methods?

A
  • The get method is a type of method that is used to retrieve the value of an object’s private attributes
  • The main purpose of a get method is to provide controlled access to the data of an object without allowing direct modification
  • Attributes are declared as private to achieve encapsulation, so they cannot be accessed directly from outside of the class
  • Instead, public get methods are used to read these attributes
19
Q

How would you code encapsulation for an ItemForSale?

A

class ItemForSale():
def __init__(self, itemName):
self.__itemName = itemName

def getItemName(self):
    return self.\_\_itemName
20
Q

What is encapsulation in terms of set methods?

A
  • Set methods are used to allow controlled access by enforcing conditions or validations before updating an attribute
21
Q

What is run-time polymorphism?

A
  • A subclass overrides a method defined in a superclass, and the correct version is chosen at run-time, not compile-time