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 or associated to other objects. Objects will interact with or be associated with other objects.
If you noticed towards the end of the video, 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 objects interacting with each other. Such an abstraction will make your code easier to read, understand, and to maintain.