What's going on Youtube,
So in today's video, we will discuss the difference between private and protected properties. This is a common question that I tend to ask interns just to see if they understand the basics of object oriented programming. While most candidates will get this question right, there will be a few that do not know.
I think that a good way to understand the differences will be to use a live example. So a few things that I want to present to you is an example of protected and private properties and why we use them.
What we have before us is a code sample that we will use to show the difference between private and protected properties. The class "Car" has the private property "vin" which is assigned to a random number. The class, "Nissan" extends from "Car" and assign the value "Nissan" to the property "make" whenever the object is created.
One thing we know about private properties is that it is only accessible by the class that it is in. Meaning that the class "Nissan" cannot see the property "vin".
One thing we know about protected properties is that it is only accessible by the class that it is in, and other classes that extends it. Meaning that the class "Nissan" can see the property "model" and "make".
If the class "Nissan" want's to access the private property "vin", then it needs to use a method that will give use that information.
When it comes to the reason behind using private and protected properties, lets just say that there is an endless list. The most common reason why developers use private and protected properties is to prevent them from being accessed improperly. Another reason why is that they are helper methods and should only be used within the class. One thing you may be wonder is: "When should I use private or protected properties?" Since the answer depends on your project, I can give you 3 short rules to remember
All getter and setter methods, should be public.
All class variables should be either protected or private
All helper method should be protected
#Music#
By Nsolo
YouTube: / @nsolo
SoundCloud: / nsolo
Watch video CS 101: Differences between Private and Protected online without registration, duration hours minute second in high quality. This video was added by user WebDev Dave 06 January 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 111 once and liked it 0 people.