OOP Flashcards

1
Q

What is Object-Oriented Programming

A

Object-Oriented Programming is a programming paradigm that relies on the concepts of classes and objects, where you break a complex program down into objects that can communicate with each other

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

What are objects?

A

Objects are a collection of data and their behaviors.
Objects frequently represent real-world objects, BUT it is possible for objects to sometimes serve application logic and have no direct real-world parallels.
Examples: authentication, templating, request handling, etc

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

Where do these objects come from?

A

Classes

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

What is a class?

A
A blueprint for creating objects.
the state of an object is modeled using variables in a class and the behavior is modeled using methods in the class
we will refer to the variables creating state for the class as properties
we will refer to the defined behavior on the class as methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are properties?

A

properties are variables that contain information regarding the object class

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

What are methods?

A

methods are functions that have access to properties and other methods of a class

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

What are the benefits of using objects and classes?

A

Objects and classes are essential for compartmentalizing code. Different components can become separate classes that would interact through interfaces. These ready-made components will also be available for use in future applications.

Classes make it easier to maintain different parts of an application since it is easier to make the changes directly on the class

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

what is the syntax for creating a class?

A

MyClass:

pass

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

what is the syntax for instantiating an object from a class

A

obj = MyClass()

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

What is a class variable?

A

a class variable is a property defined in the class and is shared among all of the instances or objects of that class. A change to a class variable will the change the value of the property on all instances or objects of the class.

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

What is an instance variable?

A

an instance variable is a property unique to the instance or object of the class. A change in the instance variable will change the value of the property on that instance or object ONLY.

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

Where are class variables defined?

A

class variables are defined outside the initializer

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

Where are instance variables defined?

A

Instance variables are defined inside the initializer

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

How do you use class variables smartly?

A

Class variables are useful when implementing properties that need to be common and accessible to all class objects

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

What are the three types of methods in Python?

A

class methods, instance methods (most used method type in Python OOP), static methods

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

What is the purpose of a method?

A

a method is an interface between the program and the properties of a class in the program.

a method can alter the content of the properties or use the values of the properties to perform an operation in the program

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

What is a method?

A

a group of statements that performs some operation that may or may not return a result

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

a special note about method parameters

A

method parameters make it possible to pass values to the method. The FIRST method parameter should always be self.

the self argument only needs to be passed in in the method definition and not when the method is called

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

a special note about the return statement in methods

A

the return statement makes it possible to get the value from the method

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

What is method overloading?

A

method overloading refers to making a method perform different operations based on the nature of its arguments.

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

What are the advantages of method overloading?

A

method overloading saves memory, as creating a new method is costlier compared to overloading a single one

increases execution speed

simple and clean code as we don’t have to keep track of different methods

allows the implementation of polymorphism

22
Q

What have we observed about OOP?

A

Classes contain properties, and objects are created to access and manipulate these properties

23
Q

What is information hiding?

A

hiding the inner workings of a class but still providing an interface for the outside world to interact with the class without knowing what’s on the inside

24
Q

What is the purpose of information hiding?

A

to prevent the instances (objects) of these class from changing the contents of a class. One class does not need to know about the underlying algorithms of another class. They can still communicate.

Information hiding is useful because it can apply simplicity to transactions between objects and classes

example: patient-doctor model. Patient doesn’t need to know the details of how the doctor comes up with their diagnoses and prescription… they just need the doctor to provide it. (personally, this might be a problem … if you ask me)

25
Q

What are the two components of data hiding?

A
1. encapsulation: binding data and the methods to manipulate that data together in a single unit, a class
a class can be thought of as a capsule having properties and methods inside it
2. abstraction
26
Q

What is encapsulation?

A

encapsulation is binding data and methods to manipulate that data together in a single unit, a class.

think of a class as a capsule with methods and properties(data)

declare all variables of the class as private to restrict direct access by code outside the class

27
Q

How do you access private properties of an encapsulated class from the outside world?

A

you must implement public methods that have access to these private properties. These methods are known as getters and setters

28
Q

What are the advantages of encapsulation?

A
  1. classes make code easy to change and maintain
  2. properties to be hidden can be specified easily
  3. we decide which outside classes or functions can access the class properties
29
Q

What is the getter method?

A

a method used to allow outside world to read the value of a property

30
Q

What is the setter method?

A

a method that allows the outside world to modify the value of a property

31
Q

What is inheritance?

A

Inheritance is creating a new class(child class) from an existing class(parent class). The new class is a specialized version of the existing class, as it inherits non-private variables and methods.

The existing class serves as a base or starting point for the new class.

32
Q

When can inheritance be used?

A

Wherever there is an IS A relationship between objects.

example: Square IS A Shape, Python IS A Programming Language

33
Q

Remind me, what is the purpose of Object Oriented Programming?

A

To be able to model real-world objects in a programming language

34
Q

What is the keyword super? More specifically, what is the super() function?

A

super refers to the parent class, from the child class. It allows you to reference the public attributes of the parent class without knowing the parent class name

3 use cases:

  1. accessing parent class properties
  2. invoking parent class methods
  3. using with initializers
35
Q

What are the different types of inheritance?

A
36
Q

What are the advantages of inheritance?

A
1. Reusability: you are able to reuse the properties and methods of a parent class in the inherited class without directly 
implementing the properties and methods
2. Code modification: it's easier to modify the code of a parent class than to modify each individual child class
3. Extensibility: rather than start from scratch, can build out a class with more properties and methods in addition to the core attributes of the parent class
4. Data hiding: private data can be hidden in the parent class using encapsulation
37
Q

What is Polymorphism?

A

refers to the same object exhibiting different forms and behaviors

We use polymorphism when we want our derived class to inherit a method from the base class and have a different implementation for it

Polymorphism is having specialized implementations of the same methods for each class

38
Q

An example of Polymorphism

A

say you create a class, Shape. The Shape class has a function to calculate the area of the shape, getArea(). Well, each shape has a different formula for calculating its area. So, instead of having to write a unique function for each specific shape, simple define a vague getArea function in the Shape class, and allow the child classes derived from the Shape class to execute it’s own implementation of the getArea function

39
Q

What is the advantage of Polymorphism?

A

Polymorphism saves the developer from unnecessary work and time.

40
Q

What is method overriding?

A

redefining a method from the parent class in a subclass

41
Q

What is Duck Typing?

A

one of the most useful concepts in OOP in Python as it allows us to implement polymorphism without using inheritance

if two objects have similar behavior, the method can be applied uniquely to both of those objects

42
Q

Why is Duck Typing useful?

A

it simplifies code and allows the engineer to implement functions without worrying about the data type

43
Q

What are abstract base classes?

A

a defined set of methods and properties that a class must implement in order to be considered a duck-type instance of that class

44
Q

What are the different relationships between classes?

A

Is-a,
Part-of (Association)
Has-a (Association)

45
Q

What is Aggregation?

A

Aggregation follows the Has-A model. This creates a parent-child relationship between two classes, with one class owning the object of another.

Aggregation is when objects have their own lifecycle and a child object can associate with only one owner object

46
Q

What makes Aggregation unique?

A

Aggregation has an independent lifetime. The Parent(owner) class only references(or points to) the Child(owned) class.

Thus, if the parent class is deleted from the program, the child class still exists and can be used

47
Q

Example of aggregation: People and their Country of Origin

A

Each person is associated with a country, but the country can exist without that person

48
Q

What is Composition?

A

Composition has a Part-of relationship model.

Composition is the process of accessing other class objects in your class. The class which creates the object of the other class is known as the owner and is responsible for the lifetime of that object.

We can achieve composition my adding smaller parts of other classes to make a complex unit.

49
Q

What makes composition unique?

A

The owner is responsible for the lifetime of the owned object. Aka, the lifetime of the owned object depends on the lifetime of the owner.

50
Q

An example of Composition: A car is composed of an engine, tires, and doors.

A

In this scenario, a car owns the objects: engine, tires, and doors.

The car is the owner class and the engine, tires, and doors are the owned classes