Object-Oriented Programming Flashcards
What are variables and functions?
Variable is a label in the program pointing to a data object
Functions is a predefined set of instructions that can involve one or more input and return zero or more output
What are attributes and methods?
Attribute is a variable associated with an object.
Method is a function associated with an object.
In Object-Oriented Programming, what is an object?
A data type which can have associated variables and functions.
An object is instantiated from a class.
What is a static method?
A method that does not have access to its instance or class inside the method
What is a class method?
A method that has access to the class but does not have access to the instance inside the method
How do you make a method a static method in Python?
Add @staticmethod
above the method definition (and remove the self
argument)
How do you make a method a class method in Python?
Add @classmethod
above the method definition (and change self
to cls
in the arguments)
How do you define class attributes?
on the lines after the class keyword, outside method definition, i.e.
class ClassName: classattribute1 = value classattribute2 = value def method(self, ...): ...
In a method, what is abstraction?
Abstraction is the concept that when using a function, the user does not need to know how the function is processing the data and returning the result.
The user only needs to know that the function has this specific purpose
Why is abstraction advantageous?
It reduces the complexity of viewing the things.
Avoids code duplication and increases reusability.
Helps to increase the security of an application or program as only important details are provided to the user.
In Object-Oriented Programming, what is encapsulation?
Encapsulation refers to the concept of bundling properties and methods together as a package.
OOP classes utilise encapsulation to hide the internal representation of an object from the outside in a process known as information hiding
Since the users need a way to access them, a set of
methods are made public
In Object-Oriented Programming, why is encapsulation advantageous?
Keeping the properties of objects private protects them from being accidentally or intentionally modified by unauthorised parties.
Makes the program logic easier to reason about / hides implementation detail / organises code for easier understanding
In OOP, what is inheritance?
Inheritance refers to the concept of properties and methods in one class being shared with its subclass.
In OOP, the subclass inherits all the properties and methods of the superclass, but the subclass may have additional functionality from the superclass with the addition of certain properties or method
Why is inheritance advantageous?
Inheritance promotes software reuse/code reuse as it allows developers to create subclasses that reuse code already declared in a superclass, reducing code duplication and saving time and resources.
What is polymorphism?
Polymorphism refers to the concept of an object being able to take on multiple forms, where inherited subclass methods can be used in different ways