Object Oriented Programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Object-Oriented Programming (OOP)

A

Paradigm where the program is composed of interacting objects, each responsible for its own data and operations

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

Class

A

Template for an object that defines its attributes and methods

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

Object

A

A specific instance of a class

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

Attribute

A

Data associated with the class

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

Method

A

A functionality of a class

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

Object state

A

The actual data values of a particular object’s attributes

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

Instantiation

A

Creation of a new object

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

How to instantiate object

A

By calling class’s constructor method → Object returned can be assigned to identifier variable

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

Types of methods

A
  • Getter methods
  • Setter methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Getter methods

A

Written as functions and return the object’s state

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

Setter methods

A

Written as procedures and used to change object’s state

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

Encapsulation

A

Wrapping all of an object’s related data and methods under one entity → Objects can’t affect the way other objects function

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

Benefits of encapsulation

A
  • Promotes modularity → Self-contained units of functionality that can be re-used
  • Reduces complexity (easier to understand how object fits into system if it’s self-contained)
  • Can change implementation of entity without impacting other parts of program
  • Improve security (information hiding)
  • Easier to maintain and test code (easier to interface with individual components)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Inheritance

A
  • Principle that allows us to create a new class that is a modified version of another class
  • Subclass gains all the attributes and methods of the superclass
  • Subclasses can be overidden and properties can be polymorphised
  • New data and behaviour can also be added to subclasses
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When to used inheritance

A
  • Is [subclass] a [superclass]?
  • E.g. is a Fox an Animal?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Benefits of inheritance

A
  • Reuse of code and avoids duplication → Saves time and effort when writing & testing
  • Hierarchy of classes → Better organisation of code
  • Specialised classes designed for specific tasks that inherit from a general class (e.g. Fox class inherits from Animal)
  • Polymorphism and overriding → ↑ Flexible and powerful code
17
Q

Overriding

A

When a subclass replaces and inherited method

18
Q

Polymorphism

A

When a subclass has a different implementation of an inherited method to the superclass

19
Q

Inheritance diagram

A

Child classes point to parent class with unfilled arrow

20
Q

Object association

A

When an object either contains or has reference to another related object (‘has a’ relationship

21
Q

Types of association

A
  • One-to-one
  • One-to-many
  • Many-to-many
  • Aggregation
  • Composition
22
Q

Aggregation

A
  • Weaker form of association
  • When an object (whole) is composed of or contains multiple other objects (parts)
  • Lifecycle of part not dependent on whole
  • E.g. Team and Player have an aggregative association
23
Q

Composition

A
  • Strong form of association (and aggregation)
  • Lifcycle of part depends on whole
  • E.g. Hotel and Room have a compositive association (rooms can’t exist if hotel is destroyed)
24
Q

Reasons to favour composition over inheritance

A
  • ↑ Flexibility (stuck with inherited attributes & methods vs Picking and choosing required properties)
  • ↑ Explicit & easier to understand (properties of composite class clearer)
  • ↑ Robust (changing superclass may change unintentionally change subclass)
  • ↑ Efficient (if subclass doesn’t use all inherited properties from superclass → Wasteful)
25
Q

Information hiding

A
  • Practice of keeping internal details of class hidden from rest of code → Prevents exposing inner workings of class
  • Implemented using access modifiers and encapsulation
  • Object’s state manipulated using getter and setter methods
26
Q

Access modifiers

A
  • Public (accessible by any object from any class in program)
  • Protected (accessible by other objects of same class or subclasses)
  • Private (only accessible to that object)
27
Q

What access modifiers to use?

A
  • Attributes should be private (prevents non-interface access)
  • Most methods should be public (allows access to interface)
28
Q

Programming to interface

A
  • Designing code to depend on interfaces rather than concrete implementations → Multiple classes can have different implementations of same method
  • Part of encapsulation
29
Q

Interface

A

A collection of abstract methods that a group of unrelated classes must implement (e.g. multiple appliances must be able to switch on and off, so this can be implemented as an interface)

30
Q

Coding interfaces

A
Public interface Switches
	Procedure SwitchOn
	Procedure SwitchOff
	Procedure SetTimer(time)
	Function GetTimer
	etc
End

Class Microwave implements Switches
31
Q

Interfaces in Python

A

Implemented as abstract classes

from abc import ABC, abstractmethod

class TwoDimensionalShape(ABC):
	@abstractmethod
	def get_area(self):
		pass
32
Q

Encapsulate what varies

A
  • Encapsulating aspects of program that are likely to change into separate classes or modules → ↑ Flexible and maintainable code
  • Separates stable parts of code from more volatile ones → Easier to understand and modify code without having to modify code that doesn’t need to change
  • Can be implemented using interface
33
Q

Advantages of OOP

A
    • Methodology forces designers to go through an extensive planning phase → better designs with fewer weaknesses
  • Encapsulation - source code for an object can be written, tested and maintained independently
  • Once created, knowledge of an object’s specific implementation isn’t necessary for a programmer to use it
  • New objects can be easily created with small differences from existing ones
  • Reusability - objects that are already defined coded and tested may be used in many different programs → OOP provides good framework for code libraries
  • Software maintenance - an object-oriented program is easier to maintain due to rigidly enforced modular structure
34
Q

UML (Unified Modelling Language) Diagrams

A

Models the classes in a system and the relationships between them

35
Q

Class Boxes

A
  • Name of class
  • Attributes : Type
  • Methods (: Return type)
36
Q

Relationship lines

A
  • Inheritance = Pointy arrow
  • Aggregation = Clear diamond arrow
  • Composition = Filled diamond arrow
37
Q

Visibility markers

A
  • Public = +
  • Private = -
  • Protected = #
38
Q

Formal class definition

A
MediaFile = Class:
Public:
Procedure PlayFile
Function GetTitle : String
Function GetDuration : Integer
Private:
Title : String
Duration : Integer

MusicFile = Subclass(MediaFile):
Public:
Function GetArtist : String
Function GetSampleRate : Integer
Funcion GetBitDepth : Integer
Private:
Artist : String
SampleRate : Integer
BitDepth : Integer