Encapsulation Case Study – Get & Set Method

As we briefly discussed Encapsulation before, we understand the basic syntax and implementation. In this post, we mainly discussed Encapsulation’s access control- get & set method, in terms of Write & Read, Write-Only and Read-Only.

In order to implement Encapsulation in our project, we need to follow two procedures:

  1. Declare field with access specifier: private
  2. Declare variable (corresponding to the field) with access specifier: public
  3. Provide variable with get and set method

get will be invoked when we attempt to read the property’s value and set will be invoked when we attempt to set the property’s value.

Case Study

Supposed we have a student class in our project and the student has his/her name and address. Both name and address are string type.

Talk Is Cheap, Show Me The Code

Basic

Above code is a basic setup for Encapsulation implementation. Field name and address have their corresponding variables, respectively.

  • get method will be called when we need to retrieve Field value.
  • set method will be called when we need to set Field value.

I used to think name and Name are two different variables and it is nothing wrong with that. However, it confuses me a lot by thinking that way. Alternatively, I treat Name is the method inside the class, which access or modify name‘s value.

Automatic Properties

C# also provides us with a simpler way to implement encapsulation, which is called Automatic Properties. We don’t have to provide property variable with field.

The code above can be written as this:

Read-Only Properties

Property contains only get accessor is referred as read-only property.

Write-Only Properties

Property contains only set accessor is referred as set-only property.

Email Subscription

66 Replies to “Encapsulation Case Study – Get & Set Method”

  1. I could not refrain from commenting. Well written! I will right away seize your rss feed as I can’t in finding your e-mail subscription hyperlink or e-newsletter service.
    Do you have any? Please allow me realize in order that I could subscribe.
    Thanks. I could not resist commenting. Well written! http://cspan.org/

    Feel free to visit my homepage: Frank

  2. I am not sure where you are getting your information,
    but good topic. I needs to spend some time learning much more or understanding more.
    Thanks for fantastic information I was looking for this information for
    my mission.

    my blog post – csgosmurfninja

  3. Ciao, articolo interessante. Noi siamo euroedile.it, il nostro
    lavoro è migliorare i cantieri dei nostri clienti.
    Se potrebbe interessarti date un occhiata al nostro sito web.

  4. Does Audible Work?
    To begin with, you receive a complimentary audiobook and take a trial
    of Audible . This really is one of the classics or an Audible originals.

    At the end of your trial, you can buy a monthly subscription of Audible.
    You have to sign up for the membership with your Amazon account.

    Audible awards you. You can use this credit to buy Audible audio books in different categories like technology, fashion, romance, social networking, advertisements, etc..

    You can buy Audible credits or pay per audio publication,
    if you would like to buy books.
    A member may download 2 of six star Originals about the first Friday of every
    month. They do not cost any credits. You can continue to keep these Audible Originals .

    You have Audible books in your library even in case you cancel your subscription.
    It is possible to listen to Audible audio books anywhere using apps
    for your telephone, Windows or Mac computer or Alexa apparatus.|
    What is Audible?
    With over 300,000 names to its name, Audible is the world’s biggest vendor and manufacturer
    of audiobooks.
    Audible Review http://uhuori.org/forums/users/jacquescastiglia/edit/?updated=true/users/jacquescastiglia/

  5. How Does Audible Function?
    First, you receive a complimentary audiobook and take a trial of Audible .
    This could be among even an originals or the classics.
    At the end of your trial, you can buy a subscription of Audible.
    You want to sign up to your membership with your Amazon account.

    are awarded by Audible with a single charge, each month.
    It is possible to use this credit to buy Audible audio books in different categories such as technology, fashion, romance, social media, advertisements, etc..

    If you wish to buy books, then you can purchase more Audible
    credits or pay-per sound publication.
    A member can download a couple of six Audible Originals in the first Friday of each month.
    They do not charge any credits. These Audible Originals can be kept by you .

    You own Audible books on your library even in the event
    you cancel your subscription.
    It’s possible to listen to Audible books anywhere using apps on Windows, your phone or Mac computer or Alexa device.|

    With more than 300,000 titles to its title, Audible is
    the world’s biggest vendor and manufacturer of audiobooks.

    Audible Review https://www.interiorsaroom.com/forums/users/dytrosie0243787/

  6. You are so awesome! I do not believe I have read through something like that before.
    So nice to find somebody with original thoughts on this issue.
    Really.. thanks for starting this up. This web site is one thing that is required on the web, someone with some originality!

    My website :: bitumen price per ton

  7. You could definitely see your skills in the work you write.
    The world hopes for more passionate writers such as you who are not afraid to mention how they
    believe. All the time go after your heart.

    Feel free to surf to my website; bitumen rate

  8. I’ve learn some just right stuff here. Defihitely worth
    bookmarking for revisiting. I wonder how a lot effort
    yoou pput to mke this sort of magnificent informative web site.

  9. Pingback: URL

Leave a Reply

Your email address will not be published. Required fields are marked *