I have been an application developer for 25 years now. When you have been programming as long as I have things become second nature and you really dont think about it all that much anymore. My daughter recently started collage for computer science. Yes i am very proud of her, she will be third generation female programmer when she finishes, her grandmother started programming in the 70’s. I graduated from collage in 94 and now my daughter Jessica is following in our footsteps.
Yesterday she sent me a message after school
I am finally starting to understand the difference between public and private in coding
While biking to work this morning I began to think how would I explain public and private to new developers so that they could grasp this vital concept as quickly as possible? So here is my attempt.
Explaining Public vs private to my daughter.
The concept of public and private things in programming is a vital concept its something you will be using daily. Its very important to understand when and why we set things to public or private. This also helps you understand why you cant access a variable that you can clearly see.
Real world example
Picture this……..
You are currently standing in my driveway looking at my house. What do you see? You can see doors, windows, the color of the house, the size of the house. These things are all public anyone can see this information by standing outside of the house. What about the doors and windows? Well assuming that the doors isn’t locked or you have a key you can use this.
Now consider that you are inside my house. What do you see now? You see the doors inside, the color of the walls, the refrigerator. Because you are in my house you can now open the refrigerator and eat my food. You can also open turn up the heat. You have access to these private items because you are within the house. You cant open the refrigerator from outside
Coding
Lets try and code up a quick example of my real world example above. Sorry Jessica I know you are learning Java but I don’t have a Java compiler on this machine so I am doing this in C#. The two languages are very close you should still be able to read this.
I have created a house class this class has two methods. The first method OpenDoor is set to public this means that it can be used outside of the house class. The second method OpenFridge is private it can only be used from within the house class.
If you look closely at the OpenDoor method you will see that it is calling OpenFridge it can because its calling it from within the house class.
public class House
{
public bool OpenDoor()
{
var canIOpenTheFridge = OpenFridge();
return true;
}
private bool OpenFridge()
{
return true;
}
}
Here is another class. Called Outside. It is able to create new house and call the OpenDoor method it can not call OpenFridge because it is outside the house.
public class Outside
{
public Outside()
{
var house = new House().OpenDoor();
}
}
Coding
Learning how to code can be quite rewarding. However it can also be hard to grasp some of the key concepts in the beginning. Take the time to learn them and understand them completely and dont ever be afraid to ask questions. This is how you will learn