hey guys this is part 2 of my
introduction to classes and objects and
in this video I'm gonna talk about how
multiple classes and objects can
interact with each other so let's get
started now just a quick recap of the
last video we created this class called
robot with the three attributes named
color and weight name was a string color
was a string as well and weight was an
integer and it had only one function
called integer itself and when this
function is run it'll just print out my
name is name whatever the name is and
then out of this class we created two
objects the first one was this one a
robot object with the name Tom color red
and weight 30 to show that it's 30
pounds and the second object was a robot
object with the name Jerry color blue
and weight 40 and we put the first
object in this variable called r1 and
then we put the second object in this
variable called r2 now let's say on top
of that we have another class here this
class is gonna be called person to
represent any person you want to
represent and it's gonna have three
attributes name which is going to be a
string personality which is also going
to be a string and in sitting which is
going to be a boolean which means this
is going to be either true or false and
if this is true that will mean that this
particular person is sitting and if this
value is false that'll mean that this
person is not sitting using this class
we'll be able to create a couple of
objects here as well and the first
object here might look like this this is
going to be a person object of course
with the name Ali's personality
aggressive and is sitting false so this
person is not sitting
she's probably standing and the second
object here might look like this this is
a person object with the name Vickie
personally personality talkative and is
sitting true and let's put the first
object here the person named Alice
to this variable called p1 and then
let's put the second object here to p2
to show that this is person 1 and this
is person 2 and let's say this person
class has a few methods or a few
functions as well sit down and stand up
all they're gonna do is gonna be pretty
simple when you run sit down
this function let's say this particular
object p1 by saying P 1 dot sit down
it'll just turn the is sitting value to
true because that person will be sitting
and then if the is sitting value is
already true when you run sit down on
that object you'll just stay at true and
the same thing with stand up it's just
gonna do the opposite so if we run stand
up on p2 by writing p2 that stand up
it's gonna turn the is sitting value to
false and then if it's already false
it's just not gonna do anything now
having these two classes and these two
sets of objects separately is fine but
is there any way to somehow make them
interact with each other and somehow
show the relationships between these two
sets of objects one situation when you
might want to do this is for example
when you want to say that a person a
particular person owns a particular
robot so for example this person named
Becky might own this robot named Tom and
this person how this might own this
other robot Jerry so how can we express
that one way to show that is having an
extra attribute to the person class and
that attribute might be called something
like robot owed this is going to be the
particular robot that's owned by this
person and this is going to be a robot
object so for example if you want to
show that Alice owns Jerry which is in
this variable called r2 you can just set
the robot own attribute of this person
object to r2 and this is going to show
that p1 owns or - and then you can do
the same thing for pizza
Becky if you want to show that this
person owns this robot which is in the
are one variable you can just set the
robot own attribute of this person to r1
and this is one very common way of
expressing the relationships between
multiple classes and objects okay let's
now see how we can actually implement
that using code I'm gonna use Java as an
example here but other languages like
Python are gonna be usually pretty
similar and let's just quickly recap
what we did in the last video we define
a class called robot by writing class
robot curly brackets and this class had
three attributes or three instance
variables string name string color and
integer weight and we also define a
constructor for creating a new object
out of this class and that one took
three arguments string and string C into
W and it created a new object and set
that object's name this nut name to N
and that object's color that new objects
color to C and that weights the object's
weight to W and this class had one
method to of course introduce self and
that one printed my name is whatever
this object's name is and then we use
the constructor that we define ourselves
to create a couple of objects the first
object had the name Tom color red and
weight 30 and so on and then we were
able to of course use this function into
yourself by writing or Wanda introduce
self and our to that inches yourself and
just a quick note if you want to
implement this yourself you'll be able
to put both of these blocks of code in a
single file and then for me I'm gonna
call it test robot and person Java and
you'll be able to find a link to this
file in the description below as well
okay moving forward to implement what I
explained earlier each person owning a
particular robot
let's create a person class by writing
of course class person curly brackets
and then this person class will have
these attributes string name string
personality and boolean is sitting and
of course boolean just means that is
sitting will be either true or false and
then to show that each person owns a
particular robot we'll need to add
another attribute of course called robot
owned and this robot own the value of
this attribute will be a robot object so
that type of this attribute will need to
be robots and then just like we did with
the robot class we can create a
constructor here as well this
constructor is gonna be called a person
because we need to call a constructor
the same name as the class so this is
person which takes three arguments
string n string P and boolean I and then
once we create this object by using this
constructor this that name that new
objects name will be and the given
string and then this new objects
personality will be P which is another
given argument and then that objects is
sitting attribute is sitting instance
variable will be the given I and let's
just quickly define the other methods we
had in this class as well one of them
was called sit down this is just going
to simply set this objects is sitting
value to true and then the opposite of
sit down which is stand up that one sets
this object is sitting instance variable
or attribute to be false and note here
that if this de is sitting is already
false when you run stand up this is not
gonna do anything because it's basically
resetting this value to be false again
okay at this point you can create a
person object by saying new person Alice
aggressive and false so this is going to
create a new person object with the name
Alice personality aggressive and is
sitting false and we can
sign that see a variable called p1 with
a type person and let's the same thing
for a second person object this person's
name will be Becky personality will be
talkative and she will be sitting so is
sitting will be qu and then we're gonna
put that in p2 after that if you want to
show that the first person here Ali's
owns the second robot here named Jerry
it's gonna be pretty simple you just
need to write p1 dot robot own equals r2
and this sets the attribute the robot
own attribute of P 1 to R 2 and then we
can do the same thing for p2 as well if
you want to show that the second person
Becky owns the first robot Tom you can
just write P 2 the robot own equals r1
and that way P to sprout on attribute
will be r1 and once you have that in
place you'll be able to do stuff like
this as well here I just wrote p1 dot
robot owned dot introduced self so
basically p1 the robot own will give us
R 2 because that's the robot p1 owns and
so this line is the same as saying R to
that engine yourself so when you execute
this line this is gonna print out my
name is Jerry because that's the name of
r2 and then we can do the same thing
with p2 here the second person so once
we write P 2 the robot own the
introduced self p2 the robot own will
give us R 1 so this is the same as
saying r1 the introduced self and of
course this line once executed will
print out my name is Tom because that's
the name of r1
so that's how you can express the
relationships between robots and person
objects but can we do something similar
within the same class
so for example what if you had these
five robots
and each of them can be expressed as a
robot object and what if you wanted to
show that for example these two robots
are friends with each other and these
two robots are friends with each other
and so on or it could be something like
this robot is looking at this other
robot and this robot is looking at this
robot and soon how can we show something
like this
well it'll actually be pretty similar to
what we saw earlier so let's take a look
at the robot class here so far it's the
same it's exactly the same as what we
had earlier we have named color and
weight as attributes and the robot
constructor and then the introduced self
function all you need to do on top of
this is you just need to add an extra
attribute called let's say looking at so
this is going to be the robot this
particular robot is currently looking at
and this is going to be a robot
object and so the type here is robot so
if you want to say for example that the
first robot tom is looking at the second
robot Jerry you just need to set the
looking at attribute of r1 to r2 you can
do that just like that by saying r1 that
looking at equals r2 and then if you
want to show also that r2 is also
looking at our one you can just write
this r2 looking at equals r1 and that's
it's the looking at attribute of r2 to
r1 so that r2 is now looking at r1 as
well so this was a little bit silly
example you know a bunch of robots just
looking at each other but you'll be able
to use exactly the same idea to
implement more realistic things too so
for example if you're trying to
implement a twitter-like system or an
Instagram like system you want to be
able to say this robot is following this
other robots or this person is following
this other person and then this person
is following this person and so to show
something like that
you can just change the name of this
looking at attribute from looking at to
following and then pretty much do
exactly the same thing as what we did
earlier okay so that wraps up my
introduction to classes and objects if
there's any related topic that you want
me to cover in the future though just
let me know in the comment below
and special thanks to brilliant dork for
sponsoring this video so in my next
video in this series I'm planning to
cover the topic of linked lists but
actually once you understood the
concepts you learn in this video you'll
be able to start learning you know more
advanced data structures like graphs and
trees so if you want to jump ahead and
start learning these concepts on your
own you might want to check out
brilliant in their computer science
algorithms course they have a section
about graphs and I think this would be
perfect for someone who's just starting
to learn data structures and algorithms
like I said in my previous videos
solving poems is a really good way to
solidify the computer science knowledge
you've gained passively by watching
videos or reading something I think
brilliant can be a good complement to
this course because it gives you a good
way to practice what you've learned
through solving problems so if you want
to complement your passive learning with
more active learning like solving
problems just go to brilliant org slash
CS dojo this will let them know that you
came from here and you get 20% of their
annual subscription through that link -
okay thanks as always for watching my
videos and thanks so much for being part
of dojo gang and I'll see you guys in
the next video