When we speak about object oriented programming encapsulation and abstraction one of the important pillars of it.
Properties are simply an additional encapsulation over our data. It make our data protected. At run time properties will be treated as combination of a simple variable, a get method and a set method. For instance when we create a property called as Age, compiler implement it by generating a variaboe called _age, a method called get_Age and method called set_Age. When we try to set something into the property internally set_Age will be called and when we try to get the value of property get_Age method will be invoked.
Question is if we can do it manually then why properties are required?
Well normally we use properties to represent data whereas we use method to represent functionality.
There is no any specific strict rule which says whether we should use property or methods. But it is recommended to use properties for storing data and methods for representing functionality.
- When it comes to new developer they find properties much easier than methods.
- Code looks simple and so become more readable.
- If everyone follows the same approach of creating methods manually, code review requirement increases. For instance one may create set_age whreas one may create method as setValueOfAge. Properties bring up consistency in data representation.