Object Oriented Programming, starts off with the concept of
classes and objects. An object is something that can perform a set of
actions/methods. A class is a blueprint of an object. For example, we can have
a blueprint of a plane like the following:
Blueprint of a plane:
-
Wings
-
Cockpit
-
Fuel Tank
-
Instructions on how to combine parts to build
plane
-
Etc
Now let us imagine that we have an actual plane. So now we
have a blueprint of a plane and an actual plane. The blueprint of a plane is a
class, while the actual plane is an object. We say that the real plane is an instance of the plane blueprint. Technically,
objects and instances are the exact same thing, but we usually use the word
instance to describe an object that is of a class.
Let’s say that we have two classes. One class is the plane
blueprint class from above. Another class is blueprint for a fighter jet we have
here:
Fighter Jet:
-
Missiles
-
Guns
-
Wings
-
Cockpit
-
Fuel Tank
-
Instructions on how to build fighter jet
-
Etc
Fighter jets are a subset of planes. Fighter jets are
planes. Also notice how Wings, Cockpit
and Fuel Tank are all shared by Fighter Jets and planes. We can say that
fighter jet is a class that inherits from class plane. So next time we can write a blueprint of a fighter jet like this:
Fighter Jet(Plane):
- Guns Missiles
- Instructions on how to build a fighter jet
- Etc
Fighter Jet(Plane):
- Guns Missiles
- Instructions on how to build a fighter jet
- Etc
One last thing to mention is that classes also have methods. On the fighter jet class, we have instructions on how to build a fighter jet. This is an initializer method (__init__) that is called whenever we want to create a fighter jet object. In addition to this method, we can also add methods such as 'Fire Missiles' or 'Fly', that allows this fighter jet object to interact with itself and with other objects.
So after doing some tweaking we have a final Fighter Jet class:
Fighet Jet(Plane)
- Guns Missiles
- Instructions on how to build a fighter jet
- Fire Missiles (This is a method)
- Fly (This is a method)
Why did people invent object oriented programming?
I’m not an incredibly experienced programmer, so I can’t
really give an expert opinion. However, my guess is that objected oriented
languages are much more efficient for the programmer. After all, with OOP, the
programmer doesn’t have to repetitively write out the same code over and over
again (e.g he doesn’t have to keep on rewriting a plane blueprint) when he can
just initialize a single object and all the code is automatically done for him.
http://antiaromatic.blogspot.ca/2015/02/lab-5-work.html?showComment=1425265486615#c6709566295146482820
A slog that I commented on. Very helpful info on Lab 5.
http://antiaromatic.blogspot.ca/2015/02/lab-5-work.html?showComment=1425265486615#c6709566295146482820
A slog that I commented on. Very helpful info on Lab 5.
No comments:
Post a Comment