top of page

C why use getters and setters

VISIT WEBSITE >>>>> http://gg.gg/y83ws?5168535 <<<<<<






Now if we need create another person, it becomes necessary to create the methods for name , age , sex all over again. Instead of doing this, we can create a bean class Person with getter and setter methods. So tomorrow we can just create objects of this Bean class Person class whenever we need to add a new person see the figure.

Thus we are reusing the fields and methods of bean class, which is much better. A very simple class that holds how much liquid is in it, and what its capacity is in milliliters. You want there to be some kind of sanity check. And worse, what if I never specified the maximum capacity? Oh dear, we have a problem. What if bottles were just one type of container? What if we had several containers, all with capacities and amounts of liquid filled?

If we could just make an interface, we could let the rest of our program accept that interface, and bottles, jerrycans and all sorts of stuff would just work interchangably.

Since interfaces demand methods, this is also a good thing. Now notice how much more robust this is. We can deal with any type of container in our code now by accepting LiquidContainer instead of Bottle. And how these bottles deal with this sort of stuff can all differ. You can have bottles that writer their state to disk when it changes, or bottles that save on SQL databases or GNU knows what else.

And all these can have different ways to handle various whoopsies. But that might be the wrong thing to do. People in comments will likely point out the flaws of this simplistic approach.

That means that they look like normal methods everywhere else does. Instead of having weird specific syntax for DTOs and stuff, you have the same thing everywhere. For example, adding validation logic to a setFoo method will not require changing the public interface of a class. From a object orientation design standpoint both alternatives can be damaging to the maintenance of the code by weakening the encapsulation of the classes.

Getter and setter methods are accessor methods, meaning that they are generally a public interface to change private class members. You use getter and setter methods to define a property. You access getter and setter methods as properties outside the class, even though you define them within the class as methods. Those properties outside the class can have a different name from the property name in the class. There are some advantages to using getter and setter methods, such as the ability to let you create members with sophisticated functionality that you can access like properties.

They also let you create read-only and write-only properties. Even though getter and setter methods are useful, you should be careful not to overuse them because, among other issues, they can make code maintenance more difficult in certain situations.

Also, they provide access to your class implementation, like public members. OOP practice discourages direct access to properties within a class. When you write classes, you are always encouraged to make as many as possible of your instance variables private and add getter and setter methods accordingly. This is because there are several times when you may not want to let users change certain variables within your classes. Creating a getter without a setter is a simple way of making certain variables in your class read-only.

Code evolves. It evolves as you develop it and learn more about the problem domain you are solving. During development classes may interact with each other than they should dependency you plan to factor out , merge together, or split apart. So I think the debate boils down to people not wanting to religiously write. Not only is getVar visually noisy, it gives this illusion that gettingVar is somehow a more complex process than it really is. There is a good reason to consider using accessors is there is no property inheritance.

See next example:. From an OO theory point of view, getters and setters are useless. The interface of your class is what it does, not what its state is. In very simple cases, where what a class does is just, e.

But in anything but very simple cases, neither the attributes nor getters and setters are part of the interface. But nobody is ever doing general OO design. And in some languages, getters and setters are far from useless. So this is really just the same case again. Some languages have stronger cultures around their conventions than others.

Hooking Interface Builder widgets to anything but ObjC properties, or using certain Java mocking libraries without getters, is just making your life more difficult. Collectives on Stack Overflow. Learn more.

Why use getters and setters in c [duplicate] Ask Question. Asked 8 years, 6 months ago. Active 8 years, 6 months ago. Viewed 4k times. Improve this question. Charlez Charlez 3 3 gold badges 10 10 silver badges 17 17 bronze badges.

Add a comment. Active Oldest Votes. Beside above, what are getters and setters and why are they important? Answer: Getters and setters are methods used to declare or obtain the values of variables, usually private ones. They are important because it allows for a central location that is able to handle data prior to declaring it or returning it to the developer. Getters and Setters are used to effectively protect your data, particularly when creating classes.

For each instance variable, a getter method returns its value while a setter method sets or updates its value. Getters and setters are also known as accessors and mutators, respectively.

The simplest way to avoid setters is to hand the values to the constructor method when you new up the object. This is also the usual pattern when you want to make an object immutable. That said, things are not always that clear in the real world.

It is true that methods should be about behavior. Category: technology and computing programming languages. I also count members of internal pimpl-classes as private if the.

Getters and Setters allow you to effectively protect your data. This is a technique used greatly when creating classes. For each variable, a get method will return its value and a set method will set the value. The getters and setters are usually public and the variables are made private.


Recent Posts

See All

留言


bottom of page