Results 1 to 6 of 6

Thread: Why to use set/get methods for private fields?

  1. #1
    Join Date
    Dec 2009
    Posts
    10

    Why to use set/get methods for private fields?

    I am beginner programmer. I have been learning programming language C++, C sharp and Java. I have read and listen from many programmers that good programming style is to never make fields of an object public. They should always be private and use set/get methods to access them from outside of the boundary of object. Why it is like this?? Only security purpose or something else also to use private specification?
    Last edited by Klaty; 05-01-2010 at 11:23 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Why to use set/get methods for private fields?

    Don't worry you will soon come through this lesson if you are learning the programming language. But for now i would say, we programmers make fields private and use set/get methods to access them, so that fields created may not lose the benefits of Polymorphism, which you would be knowing how important in OOP. Also make note that Properties can be override in other classes. and variables can not. and properties can also be used in interfaces but fields cannot be used.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Why to use set/get methods for private fields?

    I have also learned OOP(Object Oriented Programming), which you know is the basic for all the programming language. In OOP, one of the chapter/lesson is about this Private Data. This lesson tells that make data/fields private for reliability and create getter/setters to work with private data. Important to know is only define getters for values that people need to know about and setters are often not defined so that immutable objects can be created. Hope so by this information you would be able understand the problem.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Why to use set/get methods for private fields?

    Firstly would say, the private fields are used for making special protection. Provides special protection, by using a specific set/get function which allows you to keep restriction on what the field can be get/set to. User/programmer-defined messages can also shown by using this get/set methods. In big/big-scale project, you need to make private variables for security and many other programming reasons.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Why to use set/get methods for private fields?

    In order to make classes safer and easier to change, is to make fields private. As you would be knowing that constructors and methods in the same class can use the fields variables, but member or methods or constructor from outside the class can only see them. So, if this private fields wants to be used someone outside, we make get/set methods for setting and getting values. This makes your program more powerful and secure.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Why to use set/get methods for private fields?

    You would know that the fields and methods can be declared private, protected, public, or package. This are various access level , and if no access level is specified, the field or method access level is package by default. When we define private field or method then it is accessible only to the class in which it is define. So, in order to access this kind of methods or fields we make get/set methods to set and get value. Mainly private access specifier is used to increase security or to make secure the program.

Similar Threads

  1. All fields are not Updated in MS Word
    By jhon in forum Windows Software
    Replies: 5
    Last Post: 17-03-2010, 04:51 AM
  2. Show all fields except id
    By Xmen in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:56 PM
  3. How to add two fields in ms access?
    By lootera in forum Windows Software
    Replies: 3
    Last Post: 03-09-2009, 09:18 PM
  4. Access does not allow Primary key with two fields
    By Reckon in forum Networking & Security
    Replies: 2
    Last Post: 16-06-2009, 03:48 PM
  5. Convert UTF-8 fields
    By S_Asnodkar in forum Software Development
    Replies: 2
    Last Post: 09-05-2009, 12:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,859,230.62738 seconds with 16 queries