4.1.2.3 Object-oriented programming Flashcards

1
Q

What is a class?

A
A class defines methods and property/attribute fields that capture the common behaviours and characteristics of objects. A template for creating objects that determines the objects behaviour
e.g def MyClass()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an object?

A

An instance of a class, with states and behaviours

e.g x = MyClass() , x is the object

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

What is instantiation?

A

The creation of a new instance of a class. It gets its own instance variables
e.g x = MyClass()

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

What is encapsulation?

A
the process of hiding all the details of an object that do not contribute to its essential characteristics.
restrict access to methods and variables, to prevent the data from being modified by accident
"Encapsulate what varies" - hide details in the code that varies so they don't affect other parts of the program
e.g in python a class variable that should not directly be accessed should be prefixed with an underscore. e.g self.\_\_a
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is inheritance?

A
A class / subclass shares properties/fields/attributes and methods/procedures/functions of the (parent) class it is derived from;
Building a hierarchy of classes with each child class inheriting access to its parent class' methods and properties;
Relationship between two object (types) in which one object (type) is a kind of the other;
e.g class DerivedClassName(BaseClassName):
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is aggregation? (white diamond)

A
An aggregate object is one which contains other objects. If you delete the container object contents objects can live without container object, the subclass is still independent. Class (parent) and Student (child). Delete the Class and the Students still exist 
e.g 
class B(object): pass
class A(object):
    def \_\_init\_\_(self, b):
        self.b = b
b = B()
a = A(b)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is composition? (black diamond)

A

one class is container and other class is content and if you delete the container object then all of its contents objects are also deleted, the subclass isn’t independent. House (parent) and Room (child). Rooms don’t exist separate to a House.
“favour composition over inheritance”- composition is more flexible, more polymorphic
e.g
class B():
def __init__(self):
print (“hdtgshtsrt”)

class C():
    def \_\_init\_\_(self):
        self.b = B()

c = C()
c.b

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

What is polymorphism?

A
Allows functions to use objects of any of these polymorphic classes without needing to be aware of distinctions across the classes. 
For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a constructor?

A
special method of a class or structure that initializes, implicitly or explicitly, an object of that type.
e.g \_\_init\_\_
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is overriding?

A
defining in the child class a method with the same name of a method in the parent class
Method can be defined with same name; 
But have different implementation/code // perform different function;
The redefined method will be used instead of the parent’s method;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is OOP used?

A

Produces re-usable code because of inheritance/encapsulation;
Produces re-useable objects;
Data is protected // only accessible in well-defined ways (because of encapsulation);
More efficient to write programs which use pre-defined / inherited objects / classes;
Storage structure of data and method code of a class may be altered without affecting programs that make use of the class;
Code produced contains fewer errors / more reliable;
Solutions are easier to understand (when expressed in terms of objects);
Easier to enforce design consistency; easier to debug;
Less maintenance effort required by developer since objects can be re-used;
New functions can be added to objects easily (because of inheritance);

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

How is a class definition formatted?

A

ClassName = Class(ParentClass)
Public #the methods
Procedure MethodOne(Override)
Function GetAttributeOne
etc.
Private #the attributes
AttributeOne: Real
AttributeTwo: String
AttributeThree: Boolean
End

if it is a subclass, you dont have to define those methods again, but you may need to make new methods to get the new attributes

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

List two features of an object

A

Contains its own data/fields/variables/properties;
Contains its own Operations/methods/functions/procedures/behaviours/code;
Responds to messages;

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

State one reason why many programmers follow the design principle “favour composition over inheritance”.

A
With composition each class can be tested separately but it is not possible to test a subclass independently from the base class;
There can be unintended side-effects for derived classes if a method in the base class is altered;
Composition is more flexible as if a new class is developed it can easily be used instead of the class that currently is used in the composition;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the relationships aggregation and composition, making it clear what the difference between the two is.

A

Composition and aggregation are both “has a” relationships – when an object contains another object;
With composition if the containing object is destroyed so are the objects it contains, this is not the case with aggregation;

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