6.15 OOP Reference, Object, Instance and Class
Hello everyone in this course dedicated to Reference, Object, Instance and Class we will use the analogy of building a house to understand the classes. A class is actually the blueprint of a house, and using this blueprint we can build as many houses as we refresh it. As many houses as you want based on these plans. Every house you build (in other words, instantiate using the ...new operator) is an object also known as an "instance". Every house you build has an address (a physical location). In other... words, if you want to tell someone where you live, you give them your address (perhaps written on a piece... of paper). This is called a referral. You can copy this reference as many times as you want, ... but there is always only one house., we copy the paper on which the address is and not the ... house itself- same. We can pass references as parameters to constructors and methods. Let's take it a step further to... understand this here we have a House class with a color (field) instance variable. On the right side we have... the Main class with the main method. This code creates instances of the House class, changes the color... and displays the result. Let's see what happens when this code is executed. The Maison maisonRouge line = new ... House("Red"); creates a new instance of the House class.Remember that house is a plan, and we assign it to ... the variable maisonRouge. In other words, it is a reference to the object in memory. I hope that the image on the right has ... now a meaning for you. The following line Maison autreMaison = maisonRouge; creates another reference to the same object ... in memory. Here we have two references that point to the same object in memory. There is always a ... house but two references to the same object. Then we have two println instructions that display the color of maisonRouge ... and that of otherMaison. Both will display "Red" since we have two references to the same object. The next line ... calls the setCouleur method and sets the color to green. On the left, you can see that maisonRouge and autreMaison have ... both the same color now. Why? Remember that we have two references that point to the same object ... in memory. When we change the color of one of them, both references always point to the same object. In ... our real-world example, there is still only 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 instructions that display the ... color. Both now displays "green" since we always have Two references that point to the same object in memory. Notice the arrows on the left side. Now we create a new instance of the House class whose ... color is set to "White". Now we have two objects in memory but we have three references that are maisonRouge, ... autreMaison et maisonBleue. The variable (reference) maisonBleue points to a different object in memory, but maisonRouge and otherMaison point to ... the same object in memory. then, assign us to otherMaison. In other words, we dereference otherMaison. It will ... now point to a different object in memory. Before, it pointed to a "Red" color house, now it points ... to the "White" color house. In this scenario, we always have three references and two objects in memory, but ... maisonRouge points to an object while otherMaison and maisonBleus point to the same object in memory. Finally, we have ... three println instructions. The first will display "Red" since the variable maisonRouge (reference) points to the object in memory that has the ... color "Red". , while the next two lines will print "white" since otherMaison and maisonBlanc point to the same object in ... memory. I hope you have understood the concept in the next we will deepen the notion of keyword this ... and super