Chapter 58 - Use of Object-Oriented Programming (OOP) Flashcards

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

What is Object-Oriented Programming?

A

Object-Oriented Programming (OOP) attempts to capture data, information and related functionality (code) to form structured items known as objects.

In OOP, the world is viewed as a collection of objects.

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

What are objects and what can they be.

A

Objects are instances of classes (defined in the main body). They can be anything: from specific things such as cars and planes, to more abstract things such as bank accounts or data structures (stacks).

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

Object features

A

Objects have their own attributes, states and methods (behaviours)

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

Attributes

A

Attributes are variables or data members that belong to a class and represent its characteristics or state

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

State

A

A state of an object is represented by its attributes e.g the radio is on/off

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

Methods/Behaviours

A

Actions performed by an object (think of them like subroutines) often by altering attributes.

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

What is a class?

A

A class is a blueprint/template for an object and it defines the attributes and behaviours (methods) of objects in that class.

These attributes are defined as the instance variables within a class.

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

Sections of a class

A
  1. Name of the class
  2. The attributes: all the information shared with any object of this class type. Like variables.
  3. The methods/behaviours: the code associated with the class that allows you to change/access its attributes. Like subroutines
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Information hiding

A

As a general rule, attributes/instance variables are defined as private whereas most methods are defined as public. This allows other classes to access the methods but not see/change the attributes.

Therefore objects need to use messages to interact with with another object’s state.

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

What is a constructor?
What does the spec say about constructors?

A

A constructor initialises the attributes within a class.
Constructors are procedures with the name new. E.g def __init__ new (someAttributes).

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

Instantation

A

An instance of a class - the creation of an object
They have the name new when object is created. E.g my_dog = new Dog(someArguments)

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

Access the attribute “name” from the object my_dog. Then print it

A

print(my_dog.name)

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

Make the dog bark using the “bark” method. Then print

A

print(my_dog.bark())

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

Messages

A

Messages can either be getter or setter messages. In some languages, getter messages are written as functions (returning the value you wanted to get) wheras setter messages are written as procedures (usually used to change the state of the object).

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

Summary of OOP

A

Each object belongs to a class
All objects of the same class have the same structure and methods but they have their own data
Objects belonging to a class are instances of that class.

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

Encapsulation

A

This is where an object encapsulates its states (value of attributes/instance variables) and its behaviours/methods into a single entity so the attributes/behaviours of one object can’t affect those of the other.

17
Q

Inheritance

A

Classes can inherit attributes and methods from a parent class. A child in OOP is known as a subclass and a parent class is known as a superclass

18
Q

A car class inherits the attributes of a vehicle class. What would this look like?

A

class Vehicle:
def __init__new(self,doors,price):
self.doors = doors
self.price = price

class Car(Vehicle):
def __init__new(self,doors,price,wheels):
super().__init__(doors,price)
#super.new(doors,price)
self.wheels = wheels

19
Q

Polymorphism

A

Polymorphism enables methods to do different things based on the object they are acting upon. An example is overriding.

20
Q

Overriding

A

Overriding: defining a method with the same name and formal argument types as a method inherited from a superclass.

21
Q

Polymorphism example

A

class Animal:
def __init__ new (self,sound):
self.sound = sound

def makeSound(self):
pass

class Dog(Animal)
def__init__ new (self,sound):
super().__init__ new (sound)

def makeSound(self):
return f”The Dog goes {self.sound}”