Hey guys this is part one of my introduction to
classes and objects and in this video I'm going to
talk about what are classes and objects and then what
are attributes, methods, and constructors and at the end of
this video I'm going to give you some code samples
in Java but this video is going to be mostly
language independent so you'll be able to use what you
learn in this video in whatever language you're using.
Classes and objects are actually one of the most fundamental topics
when it comes to data structures and algorithms, so let's get started.
So what are classes and objects? I think
the best way to explain what they are would be
to give you a really concrete example. So let's say
as an example, you're thinking about making a website where
people can visit and talk to a bunch of robots. So what you have in mind is
when people feel lonely or something they can come visit
your website and say hi to one of the robots there.
So when a user comes to your website they might see a list of robots like this,
and here you see two of those robots and they're called Tom and Jerry.
Aside from their names they have some obvious differences.
First of all Tom's color is red, while Jerry's color
is blue as you can see and Tom is slightly
lighter in weight than Jerry with thirty pounds of weight while
Jerry is forty pounds and they both have the same
functionality. When you click this button "Introduce", Tom
will say "my name is Tom" and Jerry will say
"my name is Jerry" and that's all you have for
now and it's pretty basic but that's all you want
at the beginning when you create this website.
Now, to create a system like this you basically need a way
to represent these different entities or these different robots in
whatever programming language you're using whether it's Python, Java, or
Javascript and that's where classes and objects come in.
Now, to represent each of these robots Tom and Jerry that
we saw earlier in whatever programming language you're using you
need to store two sets of information. The first set
of information that you need to store is the properties
that each robot has. So for example the first robot
we saw had these properties: its name was Tom, the
color was red, and the weight was thirty pounds. And
the second set of information you need to store for
each of these robots is the set of functionalities it
has. As we saw earlier, it had only a single
functionality in this particular case and that was to introduce
itself and that might be represented as a function in
whatever programming language you're using and let's just call it
introduceSelf() here for now. At this point you might
say "is there any convenient way to organize these properties
and functions together?" There is, and that's called an object.
An object is basically a collection of properties which can
be expressed as variables and some functions, and with that
collection of information an object can represent some "thing", whatever that
"thing" is. So in this particular case this particular object
is representing this particular robot, Tom, but you can make
an object so that it represents a particular person, or
a particular dog, or anything else for that matter.
If you decide to represent a particular person with an object
let's say Lady Gaga just as an example, you might have
a different set of variables and a different set of functions
to represent that person. So for example, you might still
have "name", but you might also have "height" and
"weight", "eye color" and "ethnicity" and so on and your object
will probably have a different set of functions too, to
show what kind of actions that person can take. It
could be for example things like walk, sleep, talk, or
anything a person might do. OK, let's backtrack here a
little bit. Once you create an object to express this
particular robot Tom, you'll be able to put this object
in a variable just like that let's say "r1"
just as an example. You'll be able to use this
variable that contains this whole set of information.
The properties or the variables and the functions just
like any other variables for example to pass it into
a function as one of the arguments. OK, let's now
take a look at how the second robot we saw,
Jerry, can be represented with an object. It's gonna look
pretty similar to the first one it's gonna have the
same set of properties but different values this time "Jerry",
"blue", and "40", and the same function as well,
introduceSelf() and of course once you create this object, you'll
be able to put it in a variable as well
let's call that one "r2", and because you have two
distinct variables containing two different objects, you'll be able to
distinguish them very easily in your code and just quickly
about terminology, these variables within an object are called
either "instance variables" or "attributes" and these functions within
an object are called "methods". OK, so when you
see these two objects you might say well they're pretty
similar, they have the exact same set of variables or
attributes and they have the exact same set of functions
only one function in this case so you might
say "is there any convenient way to organize that somehow?"
There is, and that's what a class is essentially. Now
what's a class? A class is basically like a blueprint
from which you can make objects so just imagine for
a second someone telling you "please make a robot". If
someone tells you something like that, you say
"what kind of robot do you want? What should it
look like, and what kind of properties should it have, and
what kind of functionalities should it have?" So basically, if
you want to make a robot in sort of the
real world, you need a blueprint to make that from
to know what the robot should look like and that's
sort of how it works with programming with objects and
classes too. So when you make an object, you want
to know what kind of properties, what kind of attributes
it'll have, and what kind of functions it'll have and
you can use a class to specify exactly that.
OK, let's now see what this particular class might look like
for these two objects, these two robots. It'll probably
look like this. As you can see, it has the
same set of properties as the objects themselves "name", "color",
and "weight", but the values for these three properties are
not defined yet and that's because the class doesn't refer
to any particular object. It doesn't refer to this robot,
or this robot, in particular. It just refers to this
category of robots or this type of robot in general.
Then when you create a specific object using this
class then you can specify what the name should be,
what the color should be, and what the weight should
be and also notice here that we already have the
introduceSelf() function in this class.
You can actually implement it already when you define this
class by writing something like this. So this is just
an example from Java. In Java, for printing something on
your computer we have "System.out.println()".
It's similar to the print function or the printf
function in other languages and then inside the input
of this function we have "My name is " as a string,
plus "name" and this name will refer to whatever
the value of the name is for that particular object.
So for example if you run this function in this
object it's going to print out "My name is Tom",
because that's the name of this particular object, and
if you run this function here in this object instead, it's
going to print out "My name is Jerry" instead.
And one more thing to note here is that when
you define a class like this one you need to
give it a name. It could be anything, but let's
call it "Robot" here and as you can see the
first letter "R" is capitalized and that's a pretty
common convention when you name a Class. OK, let's now take
a look at how this whole thing might look like
in code. We're going to take a look at some
code in Java here but other languages are typically pretty
similar. Let's first assume that we already have our class
defined. Like we said, it's going to be called Robot, but
we're going to take a look at what's going to
go inside later and once you define
this class Robot, you'll be able to use this class
of course to create an object out of it. And
you can do that like this. We have "Robot
r1 = new Robot()", and this "new Robot()" says
create a new object with this class Robot, and then
we're assigning that object to r1 and then we
have the word Robot here again because a class is
used not just to create an object but also as
a type too you know just like we say "string
r1" if we want to create a string, we
can say "Robot r1" to create a reference to
a Robot object. OK, let's move on. When you create
a new object this way this new object doesn't have
the properties, doesn't have its instance variables set yet
so you need to set them after that just like
that. Here we have "r1.name = 'Tom'"
and that sets the name attribute of this object or
the name instance variable of this object to Tom and
then you need to do the same thing for the
other two instance variables by saying
"r1.color = 'red'" and "r1.weight = 30", so
that's the first object r1, let's see how we
can create a second object here. As you can see
the second block of code here is pretty similar to
the first block. We have Robot r2 = new Robot();
to create a new object and then we have
"r2.name = 'Jerry'" and so on to set
the instance variables of that object. So that's how you
can create two objects of the Robot class here but
what about the functionality we saw earlier, introduceSelf()? To
run the function for each of these objects the syntax
for that is actually pretty simple. It's going to look
like this. We have r1.introduceSelf(), this
just says run this function introduceSelf() on this object
r1. So when you run this line it's going
to print out "My name is Tom" because this robot's
name is Tom and then you could do the same
thing for r2 by writing r2.introduceSelf(),
and that's going to run this function, introduceSelf(), for
r2 and then it's going to print out
"My name is Jerry". OK let's now see how you can
actually define this class. First of all, to define what
instance variables this class will have, you can just write
these. This of course says the instance variables that this
class will have are "name", which is going to be
a string, "color", which is a string as well, and
"weight", which is an integer. And when you see other
sample code in Java, you might see words like "public",
"private", or something else before each of these attributes but
just to keep this simple I'm not going to talk
about what they are in this particular tutorial and when
you run this code it will just work just fine
without adding these words too. OK, the next thing you
need to define and implement is this function introduceSelf(). And
for that you can write this. Here, we're defining a
function called introduceSelf() and we have the "void" prefix
here to show that this function is not going to
return anything and notice that this function definition is still
inside this class and then the implementation is as we
saw earlier: System.out.println(), "My name
is this.name". Actually we could just have name
here, but I wrote this.name instead just
for emphasis. So what's this here? "This" refers to whatever
object, whatever particular object, that you're running this function on.
So if you write r1.introduceSelf(), you're
running this function on r1 so "this" will be
equivalent to r1. So it's going to be
sort of like saying System.out.println(), "My
name is r1.name", which is going to
be Tom of course. And then when you run
r2.introduceSelf(), this will refer to "this" key
word will refer to r2 instead so when you
run r2.introduceSelf() it's going to print
out "My name is r2.name", which is
Jerry. OK so that's the basics of classes and objects
in Java, but when you look at these two blocks
of code you might say well they're pretty repetitive... you
know, you need to repeat the same things over and
over again and then it's pretty cumbersome to remember to
set these attributes every time too, every time you create
an object. So you might say "is there any way
to simplify that?" There is, and it's called a constructor.
A constructor is basically a function that allows you to
create an object from a class and then you'll
be able to use the constructor to you know do
things like set the attributes for yourself. And actually you
already used a constructor, you know this line right here,
"new Robot()" that's a constructor but that's a default constructor
that Java defines for you and this constructor doesn't set
any of the attributes. So let's see how we can
create our own constructor that does set the attributes for
you. So that constructor might look like this. So if
you see this function you'll notice that you know
it looks like a regular function except for that we
don't have any prefix here like "void" or "string" for
the return value. Instead, this constructor is just
called Robot. Notice that that name is exactly the same
as the class name and then it has three arguments:
"String n", "String c", and "int w". And then inside
this function we're setting "this.name" to n,
"this.color" to c, and "this.weight" to w. As we saw
earlier, "this" refers to the particular object that we're talking
about here, in this case the particular object that has
just been created, and then we're saying that object's name
should be n, which is the given argument, and then
that object's color should be c which is the given
argument and so on. And then here, the common
practice here is to name these arguments exactly the same
as these attributes, "name", "color", and "weight", and with that
this part is going to look like "this.name = name",
"this.color = color",
"this.weight = weight", but with that naming this function, this constructor,
is doing exactly the same thing as this one so
that's really just a style issue. OK moving on, we're
almost done here... let's see how we can actually use
this constructor. Actually when you define your own constructor, your
own custom constructor, the default constructor stops working. So these
two blocks of code no longer works and then you
can use this new constructor to create new objects, just
like this. This one says "Robot r1 = new Robot("Tom", "red", 30)".
So these three arguments, "Tom",
"red", and "30", will be passed on to this constructor
and then this block of code is going to be
run and so r1's name is going to be
given "n" which is "Tom" and then the color will
be "red" and then "weight" will be 30. So this
piece of code will actually be equivalent to the
whole block of code we had earlier right here. Basically,
create a new object and then set the attributes to
the given arguments. And then we can do the same
thing for r2 as well by writing
"Robot r2 = new Robot("Jerry", "blue", 40)". This creates
a new Robot object with the name Jerry, color blue,
and weight 40. And after creating these two objects you'll
be able to use r1 and r2 just
like before by writing r1.introduceSelf() and
r2.introduceSelf(). OK, again this was just
sample code in Java, but it's pretty similar to other
languages too and you'll be able to find links to
sample code in both Java and Python in the description
below. All right, if anything was unclear in this video
please let me know in a comment below so maybe
I can talk about it in the next video. In
the next video I'm also going to talk about how
you know multiple classes and objects can interact with each other,
and with that knowledge you'll be able to finally
start building some data structures like linked lists. I'm also
planning to create a new Python tutorial video on classes
and objects, so look out for that as well. And
special thanks to brilliant.org for sponsoring this
video. You know, some of you may already be
familiar with data structures and algorithms and maybe you're watching
my videos as sort of a refresher. If that's the case,
brilliant.org might just be the right website for
you to learn more advanced concepts in computer science. So
personally, I used this computer memory course to get a
better understanding of how memory works in a computer. For
example, previously I didn't really know how structs in C
are stored on memory... I mean I had a vague
idea but I didn't really know, and I found this
course was really helpful for understanding that. Personally what I
like about Brilliant is, first of all, the fact that
they have really nice visuals like this one, you know
much nicer than mine I would say, and they're helpful
for understanding different concepts visually. Another thing I like
is the fact that they sometimes combine multiple concepts
in a single problem. What I mean by that is,
for example, for this particular problem you need to understand
different things like binary expressions, and how structs in C
are stored on the computer, and how short and char
and different types of data are stored on memory too.
And in my experience in general I have found that, you
know, solving problems is a really good way to review
different concepts at the same time and also learn new
concepts and brilliant.org definitely does a good job of
coming up with good problems to solve and they even
have pretty advanced courses like this
"Artificial Neural Networks" course on top of their standard computer
science fundamentals and algorithms courses.
You can just go to brilliant.org/csdojo
to start checking out these courses yourself and you
can get 20% off their annual subscription through that link as well.
All right, as always I'm YK from CSDojo
and you know I'm working really hard here to make
CSDojo better and better so that hopefully, eventually,
it's going to be in a class of its own.
All right I'll see you in the next video.