I'm a new programmer, so please excuse any dumbness of this question, how the following code is encapsulating private data? -
public class SomeClass
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public SomeClass(int age)
{
this.age = age;
}
}
I mean, with no restriction logic or filtering logic in the properties, how is the above code different from the folowing one -
public class SomeClass
{
public int age;
public SomeClass(int age)
{
this.age = age;
}
}
Is the first code providing any encapsulation at all?
The encapsulation process takes place on the sending computer. The de-encapsulation process takes place on the receiving computer. After doing the encapsulation, each layer uses a specific name or term to represent the encapsulated data.
The data is encapsulated in every layer at the sender's side and also de-encapsulated in the same layer at the receiver's end of the OSI or TCP/IP model.
Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.
Transport Layer--Data Encapsulation Begins When the data arrives at the transport layer, the protocols at the layer start the process of data encapsulation.
It's providing one piece of encapsulation: it's saying, "there's an Age property you can get and set, but I'm not going to tell you how I'm implementing it."
That's not very strong encapsulation, but it does keep the implementation details separate from the public API. Without changing the public API at all, you could start to store the age somewhere else - in two short
fields, in a service somewhere, as part of a long
field or whatever. You could put logging in the property to see how often it's used. You could add an event which gets fired when the age changes (that's an API change, but won't break existing callers).
EDIT: One thing to note: even though this does nothing now, the change to make it do something later is both source and binary compatible. Changing a field to become a property is not backward compatible, either in source or binary forms. In most cases it will be source-compatible, but not binary-compatible. In some cases source will no longer build. In more evil (and contrived, admittedly) both versions will build, but with different effects.
Also note that as of C# 3, you can declare a trivial property as easily as a field:
public int Age { get; set; }
I have an article about all of this which provides more details.
This is a bit of a vacuous example. As you've correctly noted, the property doesn't seem to do anything.
But it could. For example, SomeClass could put restrictions on how the Age property is modified (by say not changing the age to a bad value like -2 or 823). Also, SomeClass need not represent age as an int internally. Age could be the result of a computation (by say subtracting today's date from a person's date of birth) or it could be stored within SomeClass as another data type (say a byte, long or a double).
I mean, with no restriction logic or filtering logic in the properties, how is the above one different from the folowing one
Its not the fact that you have or have not implemented the validation logic in the property, encapsulation here means that nobody can directly access/modify your private data. The only access available is to go through the property.
Using the bottom code, anyone can genereate exceptions and cause all kinds of havoc because they can do anything they want to your data.
Using the top code as its written allows this same havoc, but at any time in the future you can implement restriction logic in the Property and not have to modify an API for users of this class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With