Object-oriented programming is one of my favorite computer science topics you could think of object-oriented programming like a PI no not that kind of pie a pie is an acronym for abstraction polymorphism inheritance and encapsulation with their powers combined they create the four pillars of object-oriented programming.
What is object-oriented programming ?
Object-oriented programming is a
programming paradigm based on the
concept of objects objects can contain
data in the form of attributes or
properties and the actions in the forms
of functions or methods.
I got that from
a quick google search basically what it
means is we're taking real world objects
and representing them within code.
Let's
take a computer monitor for example some
of its attributes could be the size or
the resolution these would be its
properties some of its actions could be
turning on or off the monitor changing
the brightness these would be functions
or methods.
The prerequisite for this is that you've worked with one
language at least that has the concept
of classes the most popular ones being
- Python
- C++
- Java
First pillar of object-oriented programming is Abstraction -
Abstraction means to only show the
necessary details to the user of the
object for instance.
We go back to
our computer monitor example when a user
is turning the monitor on or off they
don't really care about the inner
mechanisms of what's going on they just
want to push a button and see the screen
turn on and that's really what
abstraction is you're only exposing the
necessary details needed by whomever is
using the object and the best thing
about abstraction is that it really
decouples the user from the underlying
implementation.
Let's take a look at a coding example -
We have an
empty Java project
so if we want to go ahead and create a
new class and say we're building some
type of video game and we want to have
an object for enemies so let's go ahead
and create something called enemy and
right now it's completely empty but what
can we what attributes can we give to an
enemy you know maybe an enemy can have
health and here let's just give them the
ability to say something,
let's just have
him say I am an enemy you better run.
So
if we go back to our main class here. Let's create an enemy object and let's
just go ahead and invoke that talk
method so if we go up here we see that
as a user we only care about calling
this talk method we don't care about the
underlying implementation here about you
know I mean it is pretty simple in this
case but either way we don't really care
how this gets printed out for example we
could have split this into two print
statements but from a user point of view
when we call talk it still prints out
the exact same thing,
so this is the idea
of abstraction is that we only care
about calling the method we don't care
about the underlying implementation.
Second pillar of object-oriented programming is Inheritance -
Inheritance is a very powerful feature
and it basically allows you to have code
reusability.
Inheritance is useful when
you have an existing class and say you
want to build a new class that uses the
stuff from the previous class but you
want to add additional features onto it
classes which are derived from an
existing class are either called
subclass, extended class or child class
and the class from which that subclass
is derived from can be called either the
superclass, the parent or the base class.
So right now we just have this enemy
class but let's go ahead and create a
new class here say anyone have an enemy
that's like a vampire so what we want to
do is we want to take that enemy class
and we want to expand upon it with our
vampire class so what we can do here in
Java is just going to be extends enemy
and the language here can vary from
language to language but the idea is
going to be the same,
So if we go back
here we see that this class here is
completely empty but if we go back to
our main and we want to go ahead and
create a vampire object we see that V.talk exists so if we go ahead and
call that we see that down here it
prints up I am an enemy you better run
and if we go back to this vampire class
we see that this is completely empty but
because the fact that it extends the
enemy class it has the existing code
here so we don't have to rewrite it so
it reduces the amount of code that we
have to write.
Now obviously we don't
want the vampire class to say I am an
enemy you better run so what we can do
is we can override the talk method and
the way to do that is basically just by
implementing its own talk method so we
can have the vampire class say I want to
suck your blood now we see that the
vampire class inherits that talk method
from enemy but the program is gonna say
hey since this already has its own talk
method I'm actually going to go ahead
and use that so without changing
anything in the main method if we go
ahead and run that we see that it does
infact change - I want to suck your
blood.
So that right there is the idea of
inheritance all right.
The question of the day what is your favorite oriented programming language and why let me know in the comments down below.
Third pillar of object-oriented programming is polymorphism -
Polymorphism is
probably the most advanced topic out of
these four but I'm gonna try and explain
it to you guys in a way that makes sense.
Polymorphism comes from the word poly
which means many and morph or morphism
which means forms but I think that's
kind of a confusing way to look at it
basically it allows you to determine
what kind of function to run while the
program is running which will make a lot
more sense when we look at the code so
if we go back into our program here
Let's go ahead and create another class
and let's create a werewolf object let's
go ahead and have the werewolf class
extend enemy and let's go ahead and
override the talk method again and let's
go ahead and have it say I don't know
what werewolf say let's just have it say
I'm gonna bite you, so now if we go back to our main class
and let's refactor this a little bit
let's call this vampire, so let's go
ahead and invoke the talk method on both
of our objects and we see that they do
call their respective talk methods so at
this point our hierarchy is we have an
enemy as the parent class and we have
werewolf and vampire which are both
child classes.
Werewolf and vampire
are sibling classes. now let's go
ahead and comment this out so let's go
ahead and create an enemy pointer and
let's set it equal to our vampire object
and now let's go ahead and call enemy
dot talk so at this point what do you
think is gonna happen is it going to
call the talk method on the enemy class
or on the vampire class what do you guys
think let's go ahead and run that and we
see that it calls the vampire class's
talk method so an object-oriented
programming you can call methods on a
class if what's calling it is a pointer
of the parent object as we just saw in
this example here.
So let's go ahead and
look at something a little bit more
advanced so at this point we have an
array of enemy
and inside of that array we have our
vampire object and our werewolf object
now we want to invoke the talk methods
on the vampire and werewolf objects so
if we go ahead and run that we see again
that it does call the respective talk
methods and this is the power of
polymorphism is that the talk method was
figured out during runtime it looks at
what kind of object it's pointing to it
looks at the method on that and if it's
overridden it'll call the child objects
method.
Fourth pillar of object-oriented programming is Encapsulation-
Encapsulation is built on
the idea of data hiding this is where we
restrict access of certain properties or
methods of our object to whatever is
calling that object, so for example if we
go back to our code here let's go ahead
and delete everything up until our
vampire object and as we see here we
have this enemy class here let's go
ahead and make this public meaning that
we can access it from basically when
we're calling it, so when we have vampire
dot health let's go ahead and initially
set that to like 25 and then we can go
ahead and print that out so if we print
that out we see down here that it does
print out 25 so we can directly access
health from our vampire object now
generally this isn't good practice you
generally don't want whatever's calling
your object to be able to change the
properties within that object.
So what we
could do is we can go to our enemy class
and instead of making this public we can
go ahead and make this private now if we
go back to main we see that we get an
error here saying that health has
private access in enemy, so this is
mostly for safety basically you don't
have something that inadvertently
changes the property of an object but
say for some reason you do need to
change the health we can use what are
called getter and setter functions.
For
example we would go back down to our
enemy class and we would implement these
like this so right now we have two
methods we have something called get
health which simply prints out the
health and we have another one called
set health which takes in a parameter
and sets the health to that parameter so
if we go back here we can just say
instead of setting the health directly
which
call dot set health 25 and we can delete
this part here and if we want to print
it out well we can simply call vampire
dot get health and if we go ahead and
run that we should get the same result
and we do so that's really the idea of
encapsulation is that we are
encapsulating our properties within the
object and we can do that by setting our
properties to private.
So instead of just
giving everyone access to our properties
we can later on getters and setters
to give the user the control over what
we want them to be able to set and get
so those are the four pillars of
object-oriented programming abstraction
polymorphism inheritance and
encapsulation.
I hope you guys found that helpful like I mentioned it's really one of my favorite subjects in computer science I've even had interviews where the entire interview revolves around object-oriented programming and object-oriented design so it's definitely an important subject it's definitely something that I've used in the real world.
I hope you guys have an
amazing day.
0 Comments