In: Computer Science
In my object java class the question is that it is possible to have direct inputs and outputs within the setters and getters, it is highly discouraged to achieve full portability of the code. Explain why.
In the above question, it is correctly said that it is possible to have direct Access of input and output values within the setter and getter methods but when it comes to achieve full portability of the code if get highly discouraged to use setter and getter methods. This simply means that setter and getter methods in java class fail to achieve full portability of the code.
First of all let's discuss what actually setter and getter methods are:
one of the four fundamental concepts in Java is encapsulation, which is the mechanism of trapping the data members and methods together as a single unit. In this concept, the variable of a class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore it is also known as data Hiding.
To achieve encapsulation in Java:
So in Java getter and setter methods to conventional methods that are used for updating and retrieving the value of an instance variable of a class.
Instance variables are the private variable of a class so to access them we provide setters and getter methods to read and write their value. The variable which provided this method will be completely hidden from the outside class.
These methods are tend to build that allow different access levels. For example the get may be public but the set could be protected. They are actually implemented to hide the internal representation of the code while exposing the code using an alternative representation. But it allows the inheritors to change the code of how a Method can behave and it is exposed by overriding the getter and setter method from which we can express the instance variable. With the help of setter and getter methods, programmers can control over the important variables that are accessed and updated in a correct manner such that changing value of an instance variable, for specified its range.
On the other hand, the getter method is the only way for the outside world to read the particular variable value. They are used when integrating your code with some Framework-like hybrid, spring, etc. Basically internal details of a class must not be visible outside the world so we use this method but it is not necessary that every field in your class should have a heat method.
But when it comes to achieving full portability of code by setter and getter method get failed to achieve it.
First of all what is portability?
Portability of a code is a characteristic in which code is not tightly coupled to one specific platform which is coupled as early as possible to support platform specific API. While using setter and getter methods in a particular code that is going to the portable to another platform, you can't achieve full portability because setter and getter methods tend to hide internal state and require all interaction to be performed by these methods. So, third party programmers can't access internal code; they can only access that variable by these methods.
One of the disadvantages of the setter and getter methods is they provide external access to implementation detail. This is to protect complexity from the client and it is a very responsible thing to shield the source code of a program. An object method implements the behaviour and by access to these attribute, direct or indirect via getter and setter methods then the client must become aware about the internal structure of the code. So, we can say that the client is exposed to the complexity of the class. That's why an experienced object-oriented designer could probably eliminate 99% of the use of getter and setter methods in Java code without much difficulty.
In this way, We can directly access input and output data within these methods but can't achieve full portability of the particular code which is having a number of getter and setter methods.