473,372 Members | 1,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Why are plain getter/setters used?

Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; } I cannot understand it is better than below. public int x;

How do you think?

Thank you in advance.
Jul 17 '05 #1
3 18047
Liz
It provides a door that seems to be open in your
example, but allows the code owner to close the
door by adding code to the accessor.

"Kiwi" <ki**@s05.itscom.net> wrote in message
news:dc**************************@posting.google.c om...
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; }

I cannot understand it is better than below.
public int x;

How do you think?

Thank you in advance.

Jul 17 '05 #2
ki**@s05.itscom.net (Kiwi) wrote in message news:<dc**************************@posting.google. com>...
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; }

I cannot understand it is better than below.
public int x;

How do you think?

Thank you in advance.

The reason of simple getter/setter is to make room for extension. When
you make a class, you do not know whether a subtype of the class is
needed or not. If you make accesses as a method call, there is a
possibility of overriding the method call in sub class.

Actually, making a routine as a method call is good starting point for
OOP.

Regards,
Minkoo Seo
Jul 17 '05 #3
good question, but the answer goes back to the need to encapsulate, that
is "hide" details concerning the implementation of a given class from
the outside world. coming from a visual basic world, you may find this
to be unnecessary, but it is intended as a measure to reduce complexity
in code.

while having these set/get pairs may seem to be an annoyance, they give
you flexibility at later time to change the standard behavious from
"return x" or "x=" to anything more complicated that is required by
future system requirements. if your class function suddenly has to find
the value of x based on the results from another class, or even
dynamically, then this complication can be resolved by writing a more
appropaite set/get function. supported code that relies on this class
then does not have to be altered....!

know what i mean...

also, in many ways encapsulation phyloposhy comes from the expression
"keep them in the dark and feed them horse manure" also known as the
Mushroom approach.

works great for management too...

- perry
Liz wrote:
It provides a door that seems to be open in your
example, but allows the code owner to close the
door by adding code to the accessor.

"Kiwi" <ki**@s05.itscom.net> wrote in message
news:dc**************************@posting.google.c om...
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; }


I cannot understand it is better than below.
public int x;


How do you think?

Thank you in advance.



Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: kelvSYC | last post by:
I'm familiar with get and set function paradigms from Java, but what's the recommended design for such in C++? Should it be like so: foo& getFoo(); void setFoo(foo& f); or like so: foo&...
2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
2
by: Wei Wang | last post by:
Greetings, I find the JavaScript's Object.prototype and getter/setter mechanism very nice. However, I need some help with extending an object with getters/setters in the derived class. For...
13
by: Neil Zanella | last post by:
Hello, It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have...
12
by: Adam Sandler | last post by:
Hi all, I hope this is an easy one... Using VWD 2005. When I call my accessor method (getName) I always receive an empty string back. Debugging shows there should be something there but I...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
2
by: Daniel Fetchinson | last post by:
Hi list, I've been following a discussion on a new way of defining getters and setters on python-dev and just can't understand what the purpose is. Everybody agreed on the dev list that this is a...
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
1
by: mk | last post by:
It seems like getter is defined in such way that it passes only 'self': class FunDict(dict): def __init__(self): self.fundict = dict() def fget(self, fun): return fundict
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.