Is allowing to different objects or classes to have similar functionalities and behavior without writing more code, objects and classes can inherit methods, variables and more from different objects or classes as long as they have some kind of relationship between them. Even though they share common methods they can get also different methods so they won’t be exactly the same.
Let’s take a look at a bottle of whiskey. It will be our object. If you were asked to describe a bottle of whiskey, you will say that is made out of glass, it contains an alcoholic beverage that is orange reddish in color. It will make you sleepily if you drink it. Now let’s create sub-objects (there is a relationship between objects to their sub-objects, similar to the relationship between parent and child), the sub-objects will be different kinds of whiskey such as single malt, London malt, and bourbon, they all have the same definition that the object has (“whiskey”), that’s because they inherited its properties. Each one of them also has something new, which the others do not. Single malt is made in one distillery while the blended malt is made in a few distilleries. Because they all are sub-objects of the main object by definition, they inherited its properties.
Is when the same method applies to multiple classes or objects, then you can expect a similar but still a bit different outcome from both objects when the method is being executed.
We want to organize the party. It also has to be awesome. Therefore, we need to have a lot of drinks. Everybody is too busy setting things up, so we gave to Alex and Courtney each, $200 to go and bring the booze, each from a different store. Our objects then are these two heroes. They are both different from each other. They have different thoughts and feelings. So, they can represent two different objects. These objects contain a method we called “GoBringUsBooze()”. When Alex and Courtney come back, each one from a different liquor store, Alex came back with vodka and Red Bull and Courtney came back with the whiskey and wine. Now the outcome will be similar. The guests will get drunk, but each in a different way, some will feel sleepier (the whiskey and wine lovers) and some will have a feeling of great energy (vodka and Red Bull lovers). This is how we represent two different objects that have the same method, but the outcome after the method is being executed is similar but still different.
Your Dream Website Awaits!
Is a mechanism that declares whether an object will hide or share the information stored in it. If it shares, other objects can “see” it and sometimes use it. Information can be shared or hidden when the user declares “public” or “private” before the information is given.
We did not mention it. Do you remember Alex and Courtney? They have been dating for some time now. Believe it or not, Courtney is cheating on Alex! Now this information can be stored in “private” inside Courtney’s head, or it can be stored in a “public” way which means she’s telling everything to Alex (The cheating part, will represent a complex piece of information that an object contains).
If she decides to go public with it, errors in the relationship are more likely to happen, errors like Alex breaking up with her, or a lot of crying. It does not mean that something will definitely happen. However, the odds are higher. In addition, if she decides not to tell. It will be easier to do the other parts of the relationship like planning the trip they wanted or sitting down for dinner together and laugh, everything that helps their relationship (our system) to work. When other parts of the system do not get “hurt” from an object, we have ourselves a modularity that can be very effective when a programmer works on someone else’s code.
The methods can see and call each other at all times, no matter the order they were declared because their definition is recursive.
The party started, and people are starting to show up. Each person walks in and doesn’t know who is already inside. But, we were smart. We wanted everyone to know who is at the party. So we hung an empty list at the front door and each person when they came in signs his or her name. So, the second a new person comes in he will know who is already inside. That way, friends can party together. In addition, we put in a speaker. So, when someone new comes in he or she can say his or her name loudly enough. So, everyone will know that he or she has arrived. In some other paradigms, a method and variables are aware only of methods and variables that were declared before they were declared and not to the ones that were declared after.
We hope you found this article useful and amusing, don’t forget: if you are drinking, let someone else have your car keys.