OOP terms Flashcards

1
Q

Programming paradigm

A

A style of computation and programming (chosen according to the type of problem)

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

Hierarchy chart

A

A top-down, visual representation of a structured program that breaks down the overall problem into smaller sub-tasks which can be individually coded. Each procedure is represented with a rectangle, and lines show the relationships between these

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

Structured approach

A

A approach to programming that uses 4 structures to develop a program: assignment, selection, sequence and iteration
Structured programs are designed top-down and so break important elements of a problem into smaller tasks which can be solved in a block of code such as a subroutine. This makes maintenance of larger programs easier, and developing, debugging and testing easier to navigate

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

Object-orientated programming (OOP) (programming paradigm)

A

A programming paradigm where the system is viewed as a system of objects, each with their own attributes and methods that interact with each other. All processing is done by the objects

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

Class diagrams

A

Visual representations of the relationships between classes and subclasses

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

Class

A

A template/blueprint defining the attributes and methods that can be used to create objects. Classes contain the structure of the code and specify which attributes and methods objects of their type will have (i.e. objects that have been created from that class). Classes cannot themselves be executed

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

Class attribute

A

An attribute in a class which is available to all classes without inheritance
To use a class attribute in its class or a child class, the class name should be put before it, e.g. Jungle.feedback (where Jungle is the name of the class and feedback is the name of the class attribute)
When defining a class attribute in the constructor, you don’t use “self”, i.e. feedback = 5, not self.feedback = 5

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

Overriding

A

A process that occurs in polymorphism. When a method has a different implementation from the method with the same name in the parent class. A method is overridden when it has a different implementation to the method with the same name in the inherited (child) class

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

What is the purpose of a super() call?

A

It allows a method in a parent class to be used/calls a method in the parent class, even if it is overridden

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

Public attributes/methods

A

No underscore, e.g. self.name
Can be accessed from anywhere (incl. outside the class)

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

Protected attributes/methods

A

One underscore, e.g. self._name
Can be accessed from within the class and by child classes

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

Private attributes/methods

A

Two underscores, e.g. self.__name
Can only be accessed from within an object/class. Public methods can also be used to access/modify a class’s private attributes

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

Polymorphism

A

The idea that what a subclass inherits can be replaced with the subclass’s own version. A subclass can therefore override a method which it inherits from its parent/base class
In a non parent-and-child-class context, this can simply refer to two classes having a method with the same name, that do different things (but it is usually in the context of the top way)
Objects of different classes can use different methods to perform the same action

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

Inheritance

A

A child class is able to “inherit”, i.e. access, attributes/methods from its parent class, e.g. by using the super() call. This allows a class to share the properties of another class, while still having its own ones too. Inheritance is described as an “is a” relationship, i.e. a “Laptop” class would inherit from a “Computer” class because a laptop is a computer and so would share its properties, but may also have some that are specific to just itself as well
Inheritance makes code less repetitive, more organised, and avoids duplication

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

The “main” function

A

A function which uses standard-boiler-plate to tell the python interpreter that this function is the starting point of the code
The main function will contain all of the object instantiations, e.g.
def main():
r = Jungle()
r.sayname()
Standard-boiler-plate is then used to indicate this function should be run first:
if __name__ == “__main__”:
main()

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

Constructor

A

The __init__ method which defines/initialises methods and attributes for the class
The constructor of the child class is also where inheritance of attributes/methods/constructors of the parent class is done
The constructor method is executed automatically as soon as the object is created for a class (i.e. without being called)

17
Q

What are the benefits of OOP?

A
  • It increases the re-usability of code
  • Code becomes more manageable
  • The clearer structure makes it easier for testing and developing
  • The paradigm makes it easy for projects to be split among many programmers (e.g. they can all take on one class each)
  • The space-efficacy of code is good (as code can be reused in the program and used in other programs)
18
Q

Method

A

A function in a class. They can only be used once an object of the class is created. Names of functions are written with a lowercase letter

19
Q

Object

A

An instance of a class, which can be executed. They use the structure of a class to perform various tasks

20
Q

Instantiation

A

The creation of an object from a class

21
Q

Encapsulation

A

The process of combining methods and procedures to form an object (a way of maintaining data integrity)
An object is said to “encapsulate” its contents by forming a single entity encompassing specific methods/attributes
The ability to protect attributes inside a class from being accessed directly - through the use of private or protected modifiers. Instead you are forced to use public methods to interface with the attributes

22
Q

Procedural programming (programming paradigm)

A

Programs are formed from sequences of instructions executed in the order they appear. Functions and procedures make up the code and can be called from anywhere in the program or recursively
Data is stored by constants and variables. These have a “global scope” if they can be accessed from anywhere in the program, and a “local scope” if they can only be accessed from the subroutine they are in
Procedural programming uses the structured approach

23
Q

Association

A

When two objects/classes have a “has a” relationship. In this relationship, one of the objects will be the container object, which will “have” the other object as one of its associated properties, e.g. if there is a Car class and a Driver class, the Car class is the container object, and the Driver class the associated object. They have a “has a” relationship as a car “has a” driver
There are two types of association - aggregation and composition

24
Q

Aggregation

A

This is a weak form of association. When an object is associated with another through aggregation, if the containing object is destroyed, it will still exist, e.g. a car and a driver

25
Q

Composition

A

A strong form of association. When objects are associated through composition, if the containing object is destroyed, the associated one is as well, e.g. a car and a steering wheel. This means composition objects have more of a “is part of” relationship than a “has a” relationship

26
Q

What are the 3 design principles in OOP?

A
  1. Encapsulate what varies
  2. Favour composition over inheritance
  3. Program to interfaces, not implementation
27
Q

What does “encapsulate what varies” mean?

A

When designing OOP programs, any requirements that may change in the future should be encapsulated together in one class so changes can be easily made when needed

28
Q

What does “favour composition over inheritance” mean?

A

Composition should be used instead of inheritance wherever possible. This is because it is seen as a more flexible relationship between objects/classes.

29
Q

What does “program to interfaces, not implementation”

A

Interfaces are groups abstract procedures that are loosely related/coupled/dependent and simply define what the behaviour of an object should be. This design principle is basically saying that OOP should be coded with interfaces in mind, rather than focusing on how you want the specific implementation to look like. Programming to interfaces creating an interface first, defining its methods and then creating the actual class with the implementation. This provides flexibility and maintainability later on

30
Q

Abstract classes/methods

A

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code

31
Q

Overloading

A

Doing multiple implementations of a method with the same name within the same class definition. This is done by selecting which version based on the number/type of parameter passed

32
Q

“Self”

A

This is the object parameter of all classes. We know that class is a blueprint for the objects. This blueprint can be used to create multiple objects
The “self” keyword is used to represent an instance (object) of given classes. If there was no self argument, the same class couldn’t hold the information for multiple different objects.
However, since the class is just a blueprint, self allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods. Thus, even long before creating these objects, we reference the objects as self while defining the class.

33
Q

“Built-in” functions

A

Functions already available in the programming language, e.g. print, int, float, input, str

34
Q

Static methods

A

Methods which belong to the class rather than any object of a class, meaning they can be accessed without having to create a new object. These methods exist within the class itself rather than objects of the class. This could be used for example if a method was set to increment every time the class was used to instantiate an object (this process would not exist as part of the object but would just be part of the class)