Skip to content

Object-oriented

Object-oriented programming (OOP) represents data and methods by representing them with objects which communicate between them. It is component-based.

Relations

NameExplanationsExample
AssociationTwo classes are associatedCat eats Food
InheritanceA class is a sub-part of another classCat is an Animal
AggregationA class is a dependent piece of another classTail is part of Cat
CompositionA class is an independent piece of another classCat is part of a House
RealizationA class inherit from an interfaceCat do AnimalActions

Read more: Relationships in Class diagrams

Object copying

There is different ways to copy an object in OOP.

Reference copy

The object's address (reference) is copied rather than the value.

python
a = [1,2,3]
b = a
b.append(4) 

print(a) # [1,2,3,4]
print(b) # [1,2,3,4]

Shallow copy

Each field value from the object A are copying in a new empty object B.

  • Primitive are copied: changes from an object won't affect the other one
  • Other objects won't be copied, but referenced: if it changes, it will be the case for both objects
python
list1 = [1, [2, 3]] 
list2 = copy.copy(list1) # Shallow copy 

list1[0] = 10
list1[1][0] = 20

print(list1) # [10,[20,3]]
print(list2) # [1,[20,3]]

Deep copy

A complete copy of an object A to object B. The two objects are completely independent: changing one won't affect the other.