@ainasanghi: Give me 6.13 minutes I'll tea...

@ainasanghi

Give me 6.13 minutes
I'll teach you what "Object Oriented Programming" is:

Picture a boat in you head.
Got it?
Okay, great!

INHERITANCE:

What kind of boat did you picture in your head?
Is it a 1 person sunfish? A yacht?
A submarine even!?
All those are examples of boats.

So say we want to write a program that will have a sunfish, a yacht, and a submarine in it.

We could do this in a few ways.
We'd create classes for a sunfish ,a yacht and a submarine.
Sunfishes, yachts, and submarines all have attributes that they share since they are boats.
They have a top speed, and They have weight.
They have maximum passenger counts.

In each of our classes, we could define these variables, but that means we'd write the same line of code over and over.
In the sunfish class, we'd create a variable called top speed, and then in the Yatch class, we'd create a variable called top speed.
We'd do the same thing for submarines.


ENCAPSULATION:

This one is really simple!

This means that an object that exists in your program should only be able to control itself and nothing else unless EXPRESSLY stated.
Boat A should be able to tell itself to propel through the water but it shouldn't be able to control what Boat B does.

Boat B has to take care of itself.

If there is a piece of code in the BOAT class that tells itself to move we can either make it private or public ( there are others but don't worry about it for now).

Private means that only the Boat can tell itself to move.
Public means that ANY object can tell that boat to move.
Boat A telling Boat B to move doesn't make sense.
Boat B should have sole control over itself and nothing else!


POLYMORPHISM:

Say we have a program that is simulating a lot of different boats.
At the beginning of the program, all the boats are stationary and when we click the start button all the boats in the water start moving.
There are many different types of boats in our simulation: yachts, submarines, sunfishes, ocean liners, etc.

To have all the boats "go" when the user clicks the start button we can write this two ways:

One would be to tell each object individually to "go" which would look something like this.

Sunfish -> Go!

Ocean-Liner -> Go!

Submarine -> Go!

YachtA -> Go!

YachtB -> Go!

Notice that we have two yachts.
I have to call those yachts individually and tell them to go. What if our program has hundreds or THOUSANDS of boats.
That's a lot of code!!!

Instead, we use POLYMORPHISM to make things much simpler! Remember from inheritance, yachts, submarines, sunfishes, ocean liners, they are all boats!

Our code to tell all the boats to move could be something like this:

Get a list of all the objects that inherit from the base class BOAT.

Use a loop to go through each boat in that list and tell it to go!

In this case, we're telling each boat to go based on them all being a boat.
We don't care what type of boat it is.
If it's a boat then we tell it to go!


Found it helpful?

1. Follow @ainasanghi for similar content ­čî╗
2. Retweet it for your network
3. Share your views.

Thank you, that's all Bye Bye!!


Premium

Save unrolled Twitter threads to Notion.

$3/month

Try Free