Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where's the Encapsulation?

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?

like image 543
atiyar Avatar asked Sep 16 '10 15:09

atiyar


People also ask

Where does encapsulation happen?

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.

Where does encapsulation occur in OSI?

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.

What is encapsulation and where it is used?

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.

What layer does encapsulation occur?

Transport Layer--Data Encapsulation Begins When the data arrives at the transport layer, the protocols at the layer start the process of data encapsulation.


3 Answers

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.

like image 198
Jon Skeet Avatar answered Sep 22 '22 14:09

Jon Skeet


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).

like image 34
joshdick Avatar answered Sep 23 '22 14:09

joshdick


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.

like image 32
Jess Avatar answered Sep 20 '22 14:09

Jess