Slides OOP Part 1 Inheritance Reference Vs Object Vs Instance Vs Class
Slides OOP Part 1 Inheritance Reference Vs Object Vs Instance Vs Class
In this video, I'm going to discuss, and hopefully clear up any confusion about
references, vs. objects, vs. instances, vs. classes.
By now, you've probably noticed that I use the words reference, object, instance
and class frequently.
These new concepts may well be confusing at first.
In this video, I'm going to go through all these terms, and show you exactly what
each of these words mean in the context of Java programming.
We can copy that reference as many times as we like, but there is still just one
house that we're referring to.
In other words, we're copying the paper that has the address on it, not the house
itself.
We can pass references as parameters to constructors and methods.
The line House blueHouse = new House(“blue”); creates a new instance of the House class.
Remember House is a blueprint, and we are assigning it to the blueHouse variable. In other words it
is a reference to the object in memory. The image on the left hopefully makes sense to you now.
The next line House anotherHouse = blueHouse; creates another reference to the same object in
memory. Here we have two references pointing to the same object in memory. There is still one house, but
two references to that one object. In other words we have two pieces of paper with the physical address
of where the house is built (going back to our real world example).
COMPLETE JAVA MASTERCLASS
Reference vs Object vs Instance vs Class
Reference vs Object vs Instance vs Class
Next we have two println statements that print the blueHouse color and
anotherHouse color. Both will print “blue” since we have two references to the
same object.
COMPLETE JAVA MASTERCLASS
Reference vs Object vs Instance vs Class
Reference vs Object vs Instance vs Class
The next line calls the method setColor and sets the color to yellow. To the left you can that both blueHouse and anotherHouse
have the same color now. Why? Remember we have two references that point to the same object in memory. Once we change
the color, of one, both references still point to the same object. In our real world example, there is still just one physical house
at that one address, even though we have written the same address on two pieces of paper.
Here we have two println statements that are printing the color. Both now print
“yellow” since we still have two references that point to the same object in
memory. Notice the arrows on the left hand side.
COMPLETE JAVA MASTERCLASS
Reference vs Object vs Instance vs Class
Reference vs Object vs Instance vs Class
Here we are creating another new instance of the House class with the color set to “green”. Now we have two
objects in memory but we have three references which are blueHouse, anotherHouse and greenHouse. The
variable (reference) greenHouse points to a different object in memory, but blueHouse and anotherHouse
point to the same object in memory.
COMPLETE JAVA MASTERCLASS
Reference vs Object vs Instance vs Class
Reference vs Object vs Instance vs Class
Here we assigns greenHouse to anotherHouse. In other words we are dereferencing anotherHouse. It will now point
to a different object in memory. Before it was pointing to a house that had the “yellow” color, now it points to the
house that has the “green” color. In this scenario we still have three references and two objects in memory but
blueHouse points to one object while anotherHouse and greenHouse point to the same object in memory.
Finally we have three println statements. The first will print “yellow” since the blueHouse
variable(reference) points to the object in memory that has the “yellow” color, while the next two
lines will print “green” since both anotherHouse and greenHouse point to same object in memory.
My reference, the variable I call myHouse, lets me have access to that beige house
as long as my variable, myHouse, stays in scope. Or until it gets reassigned to
reference a different object.
I'm creating a red house again, but this is a different object altogether from the red
house I created on line one.
This third statement is creating yet another house object in memory which has no
relationship to the one I created on the first line.
This code has three instances of house but only two references.
That first object is said to be eligible for garbage collection immediately after that
first statement.
It's useless to the code because It's no longer accessible.
There are times we might want to instantiate an object and immediately call a
method on it.
99 percent of the time, we'll want to reference the objects we create.