Thursday, October 8, 2015

Defining Properties

Defining Properties


Properties are a natural extension of attributes. They help a class provide a public interface for getting and setting values for private attributes, while hiding the implementation.
 A property, generally, has a private data member, accompanied by accessor functions, and is accessed as a field of a class.
 A property is usually declared private in a class and a set of public set and get accessor methods provide access to the property. The get and set methods are like any other method. They can perform any program logic, throw exceptions, be overridden, and be declared with any modifier. 
A major advantage of properties is that you can run some lines of code at the time of assigning value to a variable. For example, if you want to check the range of a value before assigning it to a variable, you can use a property. 
<acces_modifier> <return_type> <property_name>
 { 
get
 //get accessor method 
{
 }
 set //set accessor method 
{
 } 

No comments:

Post a Comment