4.1.2.3 Object-oriented programming Flashcards
What is a class?
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()
What is an object?
An instance of a class, with states and behaviours
e.g x = MyClass() , x is the object
What is instantiation?
The creation of a new instance of a class. It gets its own instance variables
e.g x = MyClass()
What is encapsulation?
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
What is inheritance?
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):
What is aggregation? (white diamond)
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)
What is composition? (black diamond)
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
What is polymorphism?
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.
What is a constructor?
special method of a class or structure that initializes, implicitly or explicitly, an object of that type. e.g \_\_init\_\_
What is overriding?
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;
Why is OOP used?
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 is a class definition formatted?
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
List two features of an object
Contains its own data/fields/variables/properties;
Contains its own Operations/methods/functions/procedures/behaviours/code;
Responds to messages;
State one reason why many programmers follow the design principle “favour composition over inheritance”.
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;
Describe the relationships aggregation and composition, making it clear what the difference between the two is.
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;