Lesson 9
No Object is an Island
Chapter 2: Object interaction
Objects interact with one another
Hopefully the video was enjoyable!
So far, we have implemented object-orientation in our code, but only for single objects on their own.
In real life, however, objects do not exist on their own. They are usually related to or are associated with other objects. Objects will interact with or be associated with other objects.
Towards the end of the video earlier, you might have noticed hero.attack(enemy)
and enemy.attack(hero)
in my code. In this example, you can see how hero
can ‘interact’ with enemy
by attacking the enemy. Similarly, enemy
can also attack hero
.
In this lesson, I will start steering you towards writing codes that are even more object-oriented. More specifically, you will start developing programs not as a sequence of instructions or function calls, but as a sequence of object interactions. Such abstractions will make your code easier to read, to understand, and to maintain.