Object Oriented Programming Flashcards
(28 cards)
What does OOP stand for?
Object Oriented Programming
True or False: In OOP, an object is an instance of a class.
True
Fill in the blank: The four main principles of OOP are encapsulation, inheritance, ______, and polymorphism.
abstraction
What is the purpose of encapsulation in OOP?
To restrict access to certain components of an object and protect its state.
All you need to know is the interface of a class to use it.
Define inheritance in the context of OOP.
Inheritance allows a new class to inherit properties and methods from an existing class.
True or False: Polymorphism allows methods to do different things based on the object that it is acting upon.
True
What is a class in OOP?
A blueprint for creating objects that defines properties and methods.
What does the term abstraction mean in OOP?
Abstraction is the concept of hiding complex implementation details and showing only the essential features of an object.
What is the name for the process of creating an object from a class?
Instantiation.
What is the purpose of a ‘getter’ method in a class?
They are used to access non-public attributes from a class.
See here for using the property decorator in Python instead of getter and setter methods.
What is the purpose of a ‘setter’ method in a class?
To set the value of non-public attributes in a class.
See here for using the property decorator in Python instead of getter and setter methods.
What are the advantages of using OOP to build a software program?
- Organisation
- Reusability - inheritance reduces the need to replicate code, reduces redundancy.
- Collaboration - developers can work on different classes simultaneously.
What are the disadvantages of using OOP to build a software program?
- It can create longer programs than using a procedural approach - can slow performance.
- Greater complexity can make code more difficult to understand.
What is the relationship between a superclass and subclass?
A subclass is derived from a superclass (aka base class or parent class).
What is a helper class?
A class which is used to assist in providing functionality which is not the main objective of the application in which it is used.
What is the difference between a method and a function?
Methods are tied to objects/classes and need an instance of an object/class to be called.
Functions are independent and can be called anywhere.
How do you make a protected variable or method in Python?
By prefixing the variable or method name with _
.
What is an abstract class?
A class which is not instantiated, but used to define other classes.
~ a base class.
What is a static method?
A method defined inside a class which has no access to anything else in the class (e.g. self
)
Created using the @staticmethod
decorator in Python.
What is a class method?
A method defined inside a class which only has access to class variables and other class methods, not specific instance attributes.
Created using the @classmethod
decorator in Python.
Which constructor function is run every time a new instance of a class is created?
\_\_init\_\_()
When are try/except
blocks appropriately used in Python?
When an error cannot be avoided and you are aware of it, plus you have a specific plan to handle the error.
Should probably be limited to cases where taking in external data.
How do you create a child class in Python?
By passing the name of the parent class in parentheses after the child class name:
def parent_class(): ... def child_class(parent_class): ...
Liskov substitution principle
How does it apply to inheriting classes in Python?
Every instance of an inheriting child class is a valid instance of the parent class.